inconsistent migration history when changing a django app's name

谁都会走 提交于 2021-02-04 18:19:13

问题


I'm trying to rename one of the apps in my django website. There is another app which depends on it and its mysql tables. I went over all the files in both apps and changed the instances of the old name to the new one.

However, now I encounter this error message when trying to perform a migration:

File "/Users/Limor/anaconda/lib/python2.7/site-packages/Django-1.10a1-py2.7.egg/django/db/migrations/loader.py", line 287,
 in check_consistent_history
    migration[0], migration[1], parent[0], parent[1],
django.db.migrations.exceptions.InconsistentMigrationHistory: 
  Migration manual_tasks.0001_initial is applied before its dependency beta.0001_initial

I couldn't find a solution for this issue, and if I tried to comment out the particular function which raises the exception I run into related issues down the road. Am I doomed, or is there a way to fix it?

Thanks!

EDIT:

the old name is version_1, the new is beta and the other app which relies on it is manual_tasks.

Here's the code's structure:

~/website/
|-- .ebextensions
|   `-- django.config
|-- project
|   |-- __init__.py
|   |-- local_settings.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- db.sqlite3
|-- manage.py
|--beta
|   |-- __init__.py
|   |-- admin.py
|   |-- apps.py
|   |-- local_settings.py
|   |-- models.py
|   |-- tests.py
|   |-- urls.py
|   |-- views.py
|   |-- migrations
|       |-- __init__.py
|       |-- 0001__initial.py
|   |-- static
|       |-- assets
|       |-- images
|   |-- templates
|--manual_tasks
|   |-- __init__.py
|   |-- admin.py
|   |-- apps.py
|   |-- models.py
|   |-- tests.py
|   |-- urls.py
|   |-- views.py
|   |-- migrations
|       |-- __init__.py
|       |-- 0001__initial.py
|   |-- static
|       |-- assets
|       |-- images
|   |-- templates
`-- requirements.txt

Hope it makes more sense!


回答1:


I think you already run migration named version_1.0001_initial before you renamed app to beta. All you need is to update database records in the table django_migrations and rename old app migrations to the new name using this SQL statement:

UPDATE django_migrations SET app = 'beta' WHERE app = 'version_1';



回答2:


In my case, when I examined contents of the django_migrations table I could confirm the issue this InconsistentMigrationHistory exception was trying to raise. Thus one 0001_initial record of an applied migration for dependent application an no record for the "independent" one.

I even came to the point where the name of an application I was trying to run django-admin migrate on did not matter. That made me believe there's some kind of sanity check happening on the table before any migration operation actually takes place.

Deleting the database record and calling the migrate subcommand for the dependency first would remove the block. Both, required --fake-initial flag since all the Models were already present.




回答3:


For what it's worth, and if it helps anyone in a similar situation:

I had this issue when copying over a correct database from the production server to a test instance, to emulate the data on it, so I knew the database was correct. I haven't dug down into why this set the migrations out of line, but (and I want to emphasize that I could easily experiment on this because I had data backed up) just emptying the django_migrations table and running python manage.py migrations <app_name> --fake solved it for me.



来源:https://stackoverflow.com/questions/37627464/inconsistent-migration-history-when-changing-a-django-apps-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!