Home > front end >  How do I resolve database migration error due to changed migration filename?
How do I resolve database migration error due to changed migration filename?

Time:11-23

I'm still pretty new to Python as well as Django so I have a situation I'm not sure how to resolve.

Main issue is that on deploy of my code to dev, deployment fails, to stage or prod, it passes.

I worked on an issue where I had to drop some columns in a table in our app. After making the changes, I deployed to dev and asked for a code review. In code review, it was suggested I change the name of the migration file to something more descriptive rather than just leaving it 0018_auto_.

I made that change and deployed to dev and stage. Dev failed (when I expected it to succeed) because the new name was seen and django tried to drop columns that no longer exist. In stage, the name was never changed and the columns were dropped for the first time using that new name of the file.

So stage deploys just fine.

How do I resolve this error on dev so it recognizes this migration already took place?

Thanks!

CodePudding user response:

If you are 100% sure that the only change done for 0018 was the file name, and that it is the last migration for your app, you can do:

python manage.py migrate myapp 0017 --fake # Move to 17 without removing changes done by 18
python manage.py migrate myapp 0018 --fake # 18 is already applied, so just migrate with the new filename

This will keep the changes that are present in 0018, and just apply the changes to the filename.

CodePudding user response:

Django constructs a table named djang_migrations that keeps track of the migrations that have been applied. If you later change the name of a migration file, then it will appear as if that migration did not run.

You can rename the migration in the django_migrations table of your development environment with:

UPDATE django_migrations
SET name = '0018_new_name'
WHERE app = '%%%name_of_the_app'
  AND name = '0018_auto_'

where 0018_new_name is the new name of the migration file.

By updating this name, Django should now consider the new migration file as done.

  • Related