最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Django: Migrations not detecting changes after manually deleting migration files in production app - Stack Overflow

programmeradmin2浏览0评论

What happened:

I had an app called app with migrations up to 0012. I mistakenly deleted migration files, including 0001_initial.py. Now, when I run python manage.py makemigrations app, no new migration files are detected, even though I have made changes to the models. This issue is persisting even in a newly created app called comments, where no migration files are being detected either, despite having added new fields to the models. What I’ve tried:

I’ve ensured that the app is listed in INSTALLED_APPS. I’ve run python manage.py makemigrations app to generate migration files, but no changes are detected. I can’t drop the database as it is a production environment, and I need to preserve existing posts and data. What I need help with:

I need to figure out how to properly reset or re-sync the migrations without losing any production data. For now i have empty migrations folder with init.py innit that doesnt create any migration file. Specifically, how do I: Recreate missing migrations after deleting files manually (i.e., to match the state of the database, which still has migrations applied up to 0012). Ensure that any new apps or changes to models are correctly detected and have corresponding migration files. I’ve already checked the following:

The INSTALLED_APPS setting contains the relevant apps. Tried to use --run-syncdb did nothing. I’ve tried the makemigrations command with --empty and --verbosity 3, but it doesn't help.

Any advice on how to fix this issue without affecting production data would be greatly appreciated!

What happened:

I had an app called app with migrations up to 0012. I mistakenly deleted migration files, including 0001_initial.py. Now, when I run python manage.py makemigrations app, no new migration files are detected, even though I have made changes to the models. This issue is persisting even in a newly created app called comments, where no migration files are being detected either, despite having added new fields to the models. What I’ve tried:

I’ve ensured that the app is listed in INSTALLED_APPS. I’ve run python manage.py makemigrations app to generate migration files, but no changes are detected. I can’t drop the database as it is a production environment, and I need to preserve existing posts and data. What I need help with:

I need to figure out how to properly reset or re-sync the migrations without losing any production data. For now i have empty migrations folder with init.py innit that doesnt create any migration file. Specifically, how do I: Recreate missing migrations after deleting files manually (i.e., to match the state of the database, which still has migrations applied up to 0012). Ensure that any new apps or changes to models are correctly detected and have corresponding migration files. I’ve already checked the following:

The INSTALLED_APPS setting contains the relevant apps. Tried to use --run-syncdb did nothing. I’ve tried the makemigrations command with --empty and --verbosity 3, but it doesn't help.

Any advice on how to fix this issue without affecting production data would be greatly appreciated!

Share Improve this question asked 9 hours ago Dias OralbekovDias Oralbekov 294 bronze badges 2
  • 2 Do you have version control? Just get back the deleted migration files from there? Or does the production deployment still have those files? Get them from there then? – Abdul Aziz Barkat Commented 5 hours ago
  • @AbdulAzizBarkat, I have only 0002 in previous version – Dias Oralbekov Commented 5 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Deleting migration files complicates the process more so in a production environment. I would recommend you start by checking whether Django is managing your database tables. This defaults to True, but note that if you created your models from an existing database perhaps using the inspectdb command, it could be set to False. If False, no database table creation, modification, or deletion operations will be performed for this model. You want it to be True:

class MyModel(models.Model):
    pass
    class Meta:
        managed = True

You can also run python manage.py showmigrations to check whether those deleted files are present in the django_migrations table and which migrations have been applied. Applied migrations are marked by an [X] then run python manage.py makemigrations <app_name> followed by python manage.py migrate commands.

If Django is still not detecting changes to your models after doing the above, you can manually recreate your migration files using the --empty flag which outputs an empty migration for the specified apps, for manual editing. Do note that this is for advanced users and should not be used unless you are familiar with the migration format, migration operations, and the dependencies between your migrations:

python manage.py makemigrations <app_name> --empty --name <migration_name>

Lastly you can restore your migration files from your version control system and apply the migrations.

发布评论

评论列表(0)

  1. 暂无评论