在一个空白的MySQL数据库中,我使用:
为Django 1.11项目生成了迁移python manage.py makemigrations
我有几个自定义的相互依赖的应用程序,但生成的所有迁移都没有错误。但是,当我尝试将这些迁移应用于:
时python manage.py migrate
它适用于大多数应用迁移,但是使用以下自定义FeinCMS迁移:
Applying page.0001_initial...Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 93, in __exit__
self.execute(sql)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
return self.cursor.execute(query, args)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
不幸的是,它并没有说明哪个外键没有生成,而且迁移有几个。我尝试在迁移中重新运行它时注释掉每个字段,但是当我这样做时迁移成功。
为什么此迁移失败,我该如何解决?
答案 0 :(得分:0)
您在Django创建的表中遇到问题,表中没有INNODB ENGINE
。
在MySql中如果表有INNODB ENGINE
可以将关系作为外键,否则Django将返回错误,如问题所述
"django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')"
我有解决此问题的方法。 首先创建新的管理命令文件,如name&#34; convert_to_innodb&#34;
并在该管理命令文件中添加如下代码
from django.core.management.base import BaseCommand
from django.db import connections
class Command(BaseCommand):
def handle(self, database="default", *args, **options):
cursor = connections[database].cursor()
cursor.execute("SHOW TABLE STATUS")
for row in cursor.fetchall():
if row[1] != "InnoDB":
print "Converting %s" % row[0],
print cursor.execute("ALTER TABLE %s ENGINE=INNODB" % row[0])
和终端中的RUN命令
python manage.py convert_to_innodb
执行此命令后,您可以在已创建的表中创建外键,但
'OPTIONS': {
'init_command': 'SET default_storage_engine=INNODB',
}
在DATABASE设置中添加此配置,如下所示
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
'OPTIONS': {
'init_command': 'SET default_storage_engine=INNODB',
}
}
}
此数据库配置将创建下一次INNODB ENGINE
表,该表将适用于将来无错误
答案 1 :(得分:0)
由于合并错误,以前的迁移中存在一些错误,导致较旧的迁移与模型中的列之间断开连接。我通过以下方式解决了这个问题:
django_migrations
表格manage.py makemigrations
然后migrate
工作。