【发布时间】:2018-02-08 04:24:31
【问题描述】:
一个新的迁移,添加一个简单的表,在迁移过程中给我错误“无法添加外键约束”。
这是一个现有模型,名为EventLog:
class EventLog(models.Model):
"""
The event log.
"""
user = models.ForeignKey(User, blank=True, null=True)
timestamp = models.DateTimeField(auto_now=True)
text = models.TextField(blank=True, null=True)
ip = models.CharField(max_length=15)
metadata = JSONField(default={},blank=True)
product = models.TextField(default=None,blank=True, null=True)
type = models.ForeignKey(EventType)
def __unicode__(self):
return "[%-15s]-[%s] %s (%s)" % (self.type, self.timestamp, self.text, self.user)
def highlite(self):
if self.type.highlite:
return self.type.highlitecss
return False
这是我正在尝试创建的新模型:
class EventLogDetail(models.Model):
# NOTE: I've already tried switching 'EventLog' out for just EventLog.
eventlog = models.ForeignKey('EventLog', related_name='details')
order = models.IntegerField(default=0)
line = models.CharField(max_length=500)
class Meta:
ordering = ['eventlog', 'order']
看起来很简单,对吧?所以我进行了迁移:
./manage.py makemigrations:
Migrations for 'accounts':
accounts/migrations/0016_eventlogdetail.py
- Create model EventLogDetail
到目前为止,一切都很好。然后,我像这样迁移:
./manage.py migrate:
Operations to perform:
Apply all migrations: accounts, admin, attention, auth, contenttypes, freedns, hosting, info, mail, sessions, sites, taggit, vserver
Running migrations:
Applying accounts.0016_eventlogdetail...Traceback (most recent call last):
File "./manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/the_user/code/the_project/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 "/home/the_user/code/the_project/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 "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 93, in __exit__
self.execute(sql)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
return self.cursor.execute(query, args)
File "/home/the_user/code/the_project/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/the_user/code/the_project/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')
这就是迁移本身,总之是 Python 的荣耀:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-30 12:51
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('accounts', '0015_product_public'),
]
operations = [
migrations.CreateModel(
name='EventLogDetail',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order', models.IntegerField(default=0)),
('line', models.CharField(max_length=500)),
('eventlog', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='details', to='accounts.EventLog')),
],
options={
'ordering': ['eventlog', 'order'],
},
),
]
我已尝试重命名新模型及其中的所有内容(事件 related_name 属性),以防我使用的变量名已被某些机器采用,但结果相同。
在网上搜索,我只找到了一个与 Django 相关的这个问题的例子(Django MySQL error when creating tables),但这并没有帮助。没有要迁移到 auth 的迁移,我也不明白为什么会迁移,因为我们最近既没有弄乱那部分也没有升级任何包。
非常感谢任何帮助。
【问题讨论】:
-
您是否尝试过从“EventLog”中删除引号
-
是的,我有。没什么区别。不过谢谢。 :)