django.db.utils.OperationalError:1005,'无法创建表`xyz`。#sql-600_237`(错误:150“外键约束形成错误”)

时间:2016-11-04 18:31:38

标签: python django django-migrations

我正在尝试将一对一密钥添加到我的Django应用程序中,但是当我尝试“迁移”进程时,我总是会遇到错误(makemigrations很有效)。

require "optparse"
Version = 1.4

# Parses command line arguments
options = {}
optparse = OptionParser.new do |opts|
    # populate the options array according to the options
    ...
end

optparse.parse!

require "./connect_fb"

# start actual program
...

这就是我的模型:

  Applying xyzapp.0007_personne_extended_foreign...Traceback (most recent call last):
  File "manage.py", line 29, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 91, in __exit__
    self.execute(sql)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/xyz/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/opt/xyz/env/local/lib/python2.7/site-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 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
django.db.utils.OperationalError: (1005, 'Can\'t create table `xyz`.`#sql-600_297` (errno: 150 "Foreign key constraint is incorrectly formed")')

导致崩溃的迁移过程:

类迁移(migrations.Migration):

class PersonVolunteer(models.Model):
    person = models.OneToOneField(Person)

class Person(models.Model):
    name = models.CharField(max_length=100)

但是,即使在迁移错误后我测试它也都能正常工作。但是,如果在“迁移”期间出现该错误消息,则不应该发生这种情况。如果没有问题,可以跳过导致迁移崩溃的最后一步吗?

有人可以说我为什么会收到该错误消息以及如何解决它?

非常感谢你,祝你有个美好的一天!

2 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,可能不是更清洁,但上帝认为它有效,这对我来说是完美的。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('cvmapp', '0006_person_extended'),
    ]

    operations = [
        migrations.AddField(
            model_name='PersonVolunteer',
            name='personne',
            field=models.OneToOneField(related_name='info_volunteer', to='cvmapp.Person', db_constraint=False),
        ),
    ]

诀窍是添加&#34; db_constraint = False&#34;作为OneToOne字段的参数。

答案 1 :(得分:0)

请确保所有MySQL表都使用相同的存储引擎(即MyISM v。InnoDB)。尤其是在它们之间具有ForeignKey的表。

尽管您需要有关MySQL存储引擎的更多信息,以及如何知道使用的是哪种存储引擎,您仍需要阅读MySQL文档,尽管数据库文档中的MySQL注释也提供了一些入门信息。

我怀疑您有使用MyISAM存储引擎(MySQL <5.5的默认设置)创建的表,并且由于MySQL 5.5默认为InnoDB,并且自此之后创建了新表,因此您以混合结尾

您可以从phpMyadmin更改表存储引擎。单击您的名字,转到“操作”选项卡,然后在“表操作”框中进行更改。将所有或您的存储引擎转换为相同。