Search code examples
djangodjango-migrations

Fixtures are not working after Django squashmigrations


I just squashed a lot of migrations in Django 4.0.x. Now when running the migrations I get the following error:

  File "/opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/site-packages/Django-4.0.3-py3.9.egg/django/db/migrations/loader.py", line 120, in load_disk

    migration_module = import_module(migration_path)

  File "/opt/hostedtoolcache/Python/3.9.10/x64/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 846, in exec_module
  File "<frozen importlib._bootstrap_external>", line 983, in get_code
  File "<frozen importlib._bootstrap_external>", line 913, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/runner/work/backend/backend/general/migrations/0001_squashed_0019_merge_0018_auto_20220226_2311_0018_auto_20220228_2343.py", line 42
    code=general.migrations.0004_initial_general.Migration.load_data,
                                ^
SyntaxError: invalid decimal literal

The according code lines in the migration file is:

        migrations.RunPython(
            code=general.migrations.0004_initial_general.Migration.load_data,
        ),

I am totally lost here.


Solution

  • Look at the top of your squashed migration file - you should find a comment that reads something like this:

    # Functions from the following migrations need manual copying.
    # Move them and any dependencies into this file, then update the
    # RunPython operations to refer to the local versions:
    # general.migrations.0004_initial_general
    

    You should also have seen a notice printed to your console when you generated the migrations, alerting you that manual modification of the generated file is needed.

    This is happening because you have defined some custom migration functions in that migration, which Django was not able to migrate automatically. You need to manually copy the load_data function from 0004_initial_general.py into this squashed migration file, and then replace the reference to it in the RunPython call.