mysql-Django:1215,仅具有一个字段的模型上的“无法添加外键约束”
作者:互联网
我有一个沙拉模型:
class Salad(models.Model):
some_field = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
无论我如何调用类,单个字段,我放置什么模型(而不是Profile)或选择哪个on_delete选项.我系统地得到错误:
django.db.utils.IntegrityError:(1215,’无法添加外键约束’)
错误日志:
Traceback (most recent call last):
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
Applying playerdata.0053_auto_20180724_1943... return self.cursor.execute(sql, params)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 292, in query
_mysql.connection.query(self, query)
_mysql_exceptions.IntegrityError: (1215, 'Cannot add foreign key constraint')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.3.2\helpers\pycharm\django_manage.py", line 52, in <module>
run_command()
File "C:\Program Files\JetBrains\PyCharm 2017.3.2\helpers\pycharm\django_manage.py", line 46, in run_command
run_module(manage_file, None, '__main__', True)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "E:/Python/DjangoProjects/rolegate\manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
fake_initial=fake_initial,
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 90, in __exit__
self.execute(sql)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 117, in execute
cursor.execute(sql, params)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Div-o\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 292, in query
_mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
解决方法:
通常,这是由于您的两个相关表未使用InnoDB,并且其中至少有一个使用MyISAM引擎引起的.
问题是Foreign keys are not supported on MyISAM.
这是相关的Django票证#26777.
由于这与MYSQL完全相关,而与Django无关,因此我将链接此线程,它可能会为您提供帮助,
Converting Tables from MyISAM to InnoDB
有趣的事实
即使名称相似,我也不是负责这张Django票证的人. @SimonCharette是.
标签:django-migrations,django,mysql,django-models 来源: https://codeday.me/bug/20191108/2009767.html