为什么这个数据库迁移错误后升级我的版本django-mptt? [英] Why this database migration error after I upgrade my version django-mptt?

查看:374
本文介绍了为什么这个数据库迁移错误后升级我的版本django-mptt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django应用程序有一个requires.txt文件(。



编辑:下面这个方面的内容在2010年7月6日20时17分05分添加



通过堆栈跟踪我在5分钟前发布,我发现错误是由以下SQL语句触发的:

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

我看到了什么问题。 myapp2_mymodel 具有外键约束到 myapp5_mymodel 。但是当它试图创建这个FK时,它会失败,因为外键的目标还不存在!为什么升级后表的创建顺序不同?

解决方案

我自己解决了问题。看起来我的一个Django应用程序缺少myapp2缺少一个迁移文件夹。我修好了,事情开始正常了。


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?

解决方案

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.

这篇关于为什么这个数据库迁移错误后升级我的版本django-mptt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆