8

i use Django 1.9.2 with python 3.4.2.

In the first half of the development lifecycle i had this code:

class ModificationOrder(ERN):
...
    san_amount = models.IntegerField(default=0, \
    validators=[validate_modificationorder_san_amount])

, and i created an initial migration which resulted this line in the 0001_initial.py file:

migrations.CreateModel(
    ...
    fields = [
        ...
        ('san_amount', models.IntegerField(default=0, validators=[shop.validators.validate_modificationorder_san_amount])),
    ])

. Later i had a few migrations and i deleted the san_amount field from the model, but probably that is not related to my problem.

Now i tried to change the name of the validator to validate_resource_san_amount, but after changing it runserver results this error:

python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f8ec1a5a510>
Traceback (most recent call last):
  File "/home/csa.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check_migrations()
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 163, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/db/migrations/loader.py", line 170, in build_graph
self.load_disk()
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/db/migrations/loader.py", line 105, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name))
  File "/home/csa/.virtualenvs/sccdb34/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/csa/git/sccdb/sccdb/shop/migrations/0001_initial.py", line 12, in <module>
class Migration(migrations.Migration):
  File "/home/csa/git/sccdb/sccdb/shop/migrations/0001_initial.py", line 226, in Migration
('san_amount', models.IntegerField(default=0, validators=[shop.validators.validate_modificationorder_san_amount])),
AttributeError: 'module' object has no attribute 'validate_modificationorder_san_amount'

.

To solve the problem i guess it' d be enough to change all validate_modificationorder_san_amount to validate_resource_san_amount, but i guess conceptionally it' s a bad idea. How should i handle this problem in the right way? - So to change a validator name which is already in a migration file.

1
  • yes, make it. In such case I would also edit migration file by hands. Dec 12, 2016 at 9:11

3 Answers 3

5

Editing the migration file manually worked for me. Just go to the mentioned migration file in the errors and remove the validators part.

2
  • It might be a good idea, but unfortunately it's a too old question to remember... . Apr 23, 2020 at 7:17
  • 2
    I know. Just added it for people who may stumble upon this question now.
    – Winston
    Apr 23, 2020 at 7:52
1

Step one, go for it, rename the usage in the migration file since I doubt it would have any adverse effects from what you've said here.

But more than that, it would help to squashmigrations. Part of the steps of that as written in the docs.

Deleting all the migration files it replaces.

Updating all migrations that depend on the deleted migrations to depend on the squashed migration instead.

Removing the replaces attribute in the Migration class of the squashed migration (this is how Django tells that it is a squashed migration).

5
  • But more than that, it will help to runserver! ;-) I think the part with squashmigrations is not related to the question. Dec 12, 2016 at 9:19
  • @AlexanderTyapkov - This has nothing to do with runserver, other than thats where the op sees the error. The whole issue is that a migration file has references to code in it that no longer apply. Hence squashing migrations.
    – Sayse
    Dec 12, 2016 at 9:21
  • it was a joke. By runserver I mean that after correcting the migration file he will be able not only to squashmigrations but also hopefully to run the server. The only thing is that author didn't ask about squashing migrations. He only has one small problem in migration file. Dec 12, 2016 at 9:30
  • I just don' t get why the runserver gets broken from an old migration file? What does it do with it? What for does it check? Was it the same in the time of south? I believe not. Dec 14, 2016 at 14:42
  • @user2194805 - I didn't use south I'm afraid, I'm pretty sure runserver checks the migrations though
    – Sayse
    Dec 14, 2016 at 14:45
1

If you cannot squash away the migration yet or run into this problem when running the tests (like I did), you can work around this by leaving the old name there and calling the new function from it.

# Legacy name needed by migration 0001_initial.py
def validate_modificationorder_san_amount(value):    
    return validate_resource_san_amount(value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.