简体   繁体   中英

Django No migrations to apply, but IntegrityError on auth_permission content_type FK

I've setup a Django 1.7.8 project, ran the initial migration to setup a database without issue. Then I've brought in a few apps from another project, where there is no issue & I'm running the same version of Django.

But now when I run migrate, I get an IntegrityError after I've been told there are no migrations to apply (so why is something altering a table);

Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 165, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/core/management/sql.py", line 268, in emit_post_migrate_signal
    using=db)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 114, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/models/query.py", line 409, in bulk_create
    self._batched_insert(objs_without_pk, fields, batch_size)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/models/query.py", line 938, in _batched_insert
    using=self.db)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
    cursor.execute(sql, params)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/Users/mwalker/Sites/myproj/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`myproj`.`auth_permission`, CONSTRAINT `auth__content_type_id_508cf46651277a81_fk_django_content_type_id` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`))')

This looks like it's still the core django migrations code running so it seems incredibly difficult to debug, find an problematic app etc.

I'm also getting this from migrating an app backwards, to zero, so I assume it comes from something running at the very end of the migrate command.

To illustrate with one of my app's migration files;

class Migration(migrations.Migration):

    dependencies = [
        ('cms', '0003_auto_20140926_2347'),
    ]

    operations = [
        migrations.CreateModel(
            name='Audio',
            fields=[
                ('cmsplugin_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='cms.CMSPlugin')),
                ('caption', models.CharField(max_length=255, verbose_name='Title', blank=True)),
                ('audio_track', models.FileField(storage=project.custom_storages.MediaStorage(), upload_to=b'audio_plugin/audio')),
                ('description', models.CharField(max_length=255, null=True, verbose_name='Description', blank=True)),
                ('date_created', models.DateTimeField(default=datetime.datetime(2015, 4, 17, 10, 59, 23, 842647), blank=True)),
                ('date_modified', models.DateTimeField(default=datetime.datetime(2015, 4, 17, 10, 59, 23, 842683), auto_now=True)),
            ],
            options={
                'ordering': ['-date_created'],
                'db_table': 'audio_plugin',
                'verbose_name': 'Audio Plugin',
            },
            bases=('cms.cmsplugin',),
        ),
    ]

This migration has already been ran, but the app has no others, and after running migrate on this app, the above error is produced. And I don't think it's helpful, but the dependency can be seen on github; https://github.com/divio/django-cms/tree/develop/cms/migrations

尝试针对每个应用程序分别运行迁移:

python manage.py migrate <appname>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM