Why this database migration error after I upgrade my version django-mptt?

落花浮王杯 提交于 2019-12-10 12:31:15

问题


My Django application has a requirements.txt file (shown here) that I use to install modules in my virtual environment. Everything works fine.

However, I'm now trying to upgrade django-mptt from 0.6.1 to the latest version. (I actually don't care to upgrade django-mptt. I just want to upgrade my version of Django. But it seems to upgrade Django, I must first upgrade django-mptt as described here). So I do pip install -U django-mptt. This causes django-mptt to go from 0.6.1 to 0.7.4 and Django to go from 1.7.1 to 1.8.2. And it also causes django-cache-machine to from origin to master. You can see the changes in the screenshot below.

Then when I do manage.py runserver It then prompts me to migrate. So I do that. No problems. However subsequently if I drop all the tables and then run migrate again, I get this error during migration:

django.db.utils.OperationalError: 
(1005, 'Can\'t create table `mydb_instance`.`#sql-21b_1e` 
(errno: 150 "Foreign key constraint is incorrectly formed")')

Full stack trace is here.

What is this error? Does it have something to do with the fact that I'm using MariaDB (Server version: 10.0.15-MariaDB Homebrew) instead of MySQL as my Database?

EDIT: THE PORTION BELOW THIS POINT WAS ADDED on July 6,2015 at 5:28 UTC

I mentioned above that after the upgrade, I was prompted to do a migrate. Strangely, this occurred even though upgrading MPTT doesn't cause any new migration files to be created! When I did manage.py runserver, I got the following warning message. Why? It makes no sense:

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

EDIT: THE PORTION BELOW THIS POINT WAS ADDED on JULY 6,2015 at 17:00 UTC

As an experiment, I switched out the underlying database from MariaDB to MySQL (Server version: 5.6.25). The problem persisted. The error I got was Cannot add foreign key constraint. The full stack trace is here.

EDIT: THE PORTION BELOW THIS POINT WAS ADDED on JULY 6,2015 at 17:05 UTC

Going through the stack trace I posted 5 minutes ago, I found the error was being triggered by the following SQL statement:

ALTER TABLE `myapp2_mymodel2` ADD CONSTRAINT `mymod_mymodel5_id_335ee73cecd6ecbf_fk_myapp5_mymodel5_id` FOREIGN KEY (`mymodel5_id`) REFERENCES `myapp5_mymodel5`;

I see what the issue is. myapp2_mymodel has a foreign key constraint to myapp5_mymodel. However when it tries to create this FK, it fails because the target of the foreign key doesn't yet exist! Why is the order of table creation different after the upgrade?


回答1:


I solved the problem myself. It looks like one of my Django applications lacked myapp2 lacked a migrations folder. I repaired it and things started working fine.



来源:https://stackoverflow.com/questions/31177651/why-this-database-migration-error-after-i-upgrade-my-version-django-mptt

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