A new migration, adding a single, simple table, gives me the error "Cannot add foreign key constraint" during migration.
Here's an existing model, called 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
Here is then the new model, which I'm trying to create:
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']
Seems simple enough, right? So I make the migration:
./manage.py makemigrations
:
Migrations for 'accounts':
accounts/migrations/0016_eventlogdetail.py
- Create model EventLogDetail
So far, so good. Then, I migrate, like so:
./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')
Here then is migration itself, in all is Pythonic glory:
# -*- 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'],
},
),
]
I've tried renaming the new model and everything in it (event the related_name
attribute), in case I were using variable names already taken by some machinery under hood, but with the same results.
Searching online, I've only found one example of this problem with Django specifically (Django MySQL error when creating tables), but that didn't help. There are no migrations to be made to auth
, nor can I see why there would be, since we have neither messed with that part nor upgraded any of the packages lately.
Any help deeply appreciated.