简体   繁体   English

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

[英]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. 我已经设置了Django 1.7.8项目,并进行了初始迁移以设置数据库而没有任何问题。 Then I've brought in a few apps from another project, where there is no issue & I'm running the same version of Django. 然后,我从另一个项目引入了一些应用程序,那里没有问题,并且我正在运行相同版本的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); 但是现在当我运行迁移时,在被告知没有要应用的迁移之后,我会收到一个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`))')

This looks like it's still the core django migrations code running so it seems incredibly difficult to debug, find an problematic app etc. 看起来它仍然是django迁移的核心代码,因此似乎难以调试,查找有问题的应用程序等。

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

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; 而且我认为这没有帮助,但是可以在github上看到依赖关系。 https://github.com/divio/django-cms/tree/develop/cms/migrations https://github.com/divio/django-cms/tree/develop/cms/migrations

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

python manage.py migrate <appname>

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

相关问题 Django-如何使用auth_permission模型 - Django - How to work with `auth_permission` Model Django:如何从权限模型中获取所有权限(auth_permission) - Django: How to get all perimssions from Permission Model (auth_permission) 测试时图像的Django content_type - django content_type of a image while testing 在两个Django项目之间共享数据库时如何解决“表“auth_permission”已经存在”错误 - How to solve "table "auth_permission" already exists" error when the database is shared among two Django projects Django 按名称查询 content_type 而不是 content_type_id - Django query content_type by name not content_type_id 没有要应用的迁移,但Django仍在尝试创建新的内容类型 - No migrations to apply but Django is still trying to create a new content type 如何从 Django 模型类获取 content_type? - How to obtain content_type from a Django Model Class? Django“文件”对象没有属性“ content_type” - Django 'File' object has no attribute 'content_type' 尝试在Django中使用Content_type自然键加载灯具时输入错误 - Type error trying to load fixtures with Content_type natural keys in Django 表单级别与模型级别的Django文件大小和content_type限制? - Django file size & content_type restriction at form level vs model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM