首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建表时的Django MySQL错误

创建表时的Django MySQL错误
EN

Stack Overflow用户
提问于 2015-02-17 12:26:03
回答 11查看 24.6K关注 0票数 35

我正在用MySQL DB构建一个django应用程序。当我第一次运行'python manage.py迁移‘时,会很好地创建一些表,然后就会出现一些错误。所提出的错误是:

django.db.utils.IntegrityError:(1215,“无法添加外键约束”)

当我运行这个MySQL命令时-

显示引擎INNODB状态\G,

我得到了这个>>>

代码语言:javascript
运行
复制
2015-02-17 14:33:17 7f10891cf700 Error in foreign key constraint of table movie_store/#sql-4f1_66:
 FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`):
Cannot resolve table name close to:
 (`id`)

完整的回溯是:

代码语言:javascript
运行
复制
Creating tables...
    Creating table users
    Creating table merchant
    Creating table celery_taskmeta
    Creating table celery_tasksetmeta
    Creating table djcelery_intervalschedule
    Creating table djcelery_crontabschedule
    Creating table djcelery_periodictasks
    Creating table djcelery_periodictask
    Creating table djcelery_workerstate
    Creating table djcelery_taskstate
    Creating table post_office_email
    Creating table post_office_log
    Creating table post_office_emailtemplate
    Creating table post_office_attachment
    Running deferred SQL...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 173, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 309, in sync_apps
    cursor.execute(statement)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
EN

回答 11

Stack Overflow用户

发布于 2015-09-08 09:57:33

这会起作用的

代码语言:javascript
运行
复制
python manage.py migrate auth
python manage.py migrate

这个问题是因为其他迁移在auth之前运行的,所以这将确保“authtools”的迁移首先运行。

票数 39
EN

Stack Overflow用户

发布于 2017-03-03 18:00:50

我在使用时遇到了这个问题:

代码语言:javascript
运行
复制
$ python manage.py test

如果您没有对那些模型进行迁移,而这些模型的字段是django.contrib.auth.models.User,的Foreignkey,那么就会导致这个问题。

如果启用了--keepdb,就会发现没有auth_user表和其他django的管理表。

让我们追踪整个问题:

跑:

代码语言:javascript
运行
复制
$ python manage.py test --verbosity=3

您可以看到在其后引发的foreigngkey约束异常。

运行延迟SQL..。

延迟sql类似于

"ALTER xxx添加约束xx外键(x)引用auth_user

检查django/核心/管理/命令/迁移。的源代码

代码语言:javascript
运行
复制
for statement in deferred_sql:
    cursor.execute(statement)

defered_sql来自manifest.items() for循环,

manifest来自all_models

all_models来自app_config.label in app_labels

这是通过的论点

代码语言:javascript
运行
复制
self.sync_apps(connection, executor.loader.unmigrated_apps)

因此,executor.loader.unmigrated_apps将包含未迁移的app标签,如果您碰巧对Django的auth_user拥有Foreignkey,它将导致Foreignkey约束错误,此时没有名为auth_user的表。

解决办法:

假设app是包含那些Foreignkey属性类的模块:

代码语言:javascript
运行
复制
$ python manage.py migrate auth
$ python manage.py migrate
$ python manage.py makemigrations app

而且,如果您有依赖于app的其他模块,假设数据库表与app模块具有相同的字段,则需要:

代码语言:javascript
运行
复制
$ python manage.py make migrate app --fake
票数 11
EN

Stack Overflow用户

发布于 2015-07-22 14:59:08

您是否为所有的应用程序创建了迁移?如果没有,您很可能遇到了这样的问题:数据库表是以错误的顺序创建的,这将给您带来错误。

如果您有一个现有的Django 1.7项目,那么您需要创建初始迁移文件,然后伪造初始迁移,如下所述

https://docs.djangoproject.com/en/1.8/topics/migrations/#adding-migrations-to-apps

创建迁移的

代码语言:javascript
运行
复制
$ python manage.py make migrations your_app_label

然后伪造申请

代码语言:javascript
运行
复制
$  python manage.py migrate --fake-initial your_app_label
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28561458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档