升级版本django-mptt后为什么会出现此数据库迁移错误?

时间:2022-03-18 19:24:09

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

我的Django应用程序有一个requirements.txt文件(如下所示),用于在我的虚拟环境中安装模块。一切正常。

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.

但是,我现在正在尝试将django-mptt从0.6.1升级到最新版本。 (我实际上并不关心升级django-mptt。我只想升级我的Django版本。但它似乎升级了Django,我必须首先升级django-mptt,如此处所述)。所以我做pip install -U django-mptt。这导致django-mptt从0.6.1变为0.7.4,Django从1.7.1变为1.8.2。它还会导致django-cache-machine从origin到master。您可以在下面的屏幕截图中看到更改。

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:

然后,当我执行manage.py runserver时,它会提示我进行迁移。所以我这样做。没问题。但是,如果我删除所有表然后再次运行迁移,则在迁移期间出现此错误:

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?

这个错误是什么?是否与我使用MariaDB(服务器版本:10.0.15-MariaDB Homebrew)而不是MySQL作为我的数据库这一事实有关?

升级版本django-mptt后为什么会出现此数据库迁移错误?

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

编辑:以下部分该点已于2015年7月6日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:

我在上面提到升级后,我被提示进行迁移。奇怪的是,即使升级MPTT不会导致创建任何新的迁移文件,也会发生这种情况!当我执行manage.py runserver时,收到以下警告消息。为什么?这个不成立:

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

编辑:以下部分该部分已于2015年7月6日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.

作为实验,我将基础数据库从MariaDB切换到MySQL(服务器版本:5.6.25)。问题依然存在。我得到的错误是无法添加外键约束。完整的堆栈跟踪在这里。

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

编辑:这个点以下这个点在2015年7月6日在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:

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

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?

我知道问题是什么。 myapp2_mymodel对myapp5_mymodel有一个外键约束。但是当它尝试创建此FK时,它会失败,因为外键的目标尚不存在!为什么升级后表创建顺序不同?

1 个解决方案

#1


0  

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应用程序缺少myapp2缺少一个迁移文件夹。我修好了,事情开始正常。

#1


0  

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应用程序缺少myapp2缺少一个迁移文件夹。我修好了,事情开始正常。