繁体   English   中英

Django没有要应用的迁移,但是auth_permission content_type FK上出现IntegrityError

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

我已经设置了Django 1.7.8项目,并进行了初始迁移以设置数据库而没有任何问题。 然后,我从另一个项目引入了一些应用程序,那里没有问题,并且我正在运行相同版本的Django。

但是现在当我运行迁移时,在被告知没有要应用的迁移之后,我会收到一个IntegrityError (所以为什么要更改表)?

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`))')

看起来它仍然是django迁移的核心代码,因此似乎难以调试,查找有问题的应用程序等。

我还通过将应用程序向后迁移到零来实现这一点,因此我认为它来自在migration命令末尾运行的某些东西。

用我的应用程序的一个迁移文件进行说明;

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',),
        ),
    ]

此迁移已运行,但该应用没有其他应用,在此应用上运行迁移后,会产生上述错误。 而且我认为这没有帮助,但是可以在github上看到依赖关系。 https://github.com/divio/django-cms/tree/develop/cms/migrations

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

python manage.py migrate <appname>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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