我已经设置了一个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迁移代码,因此调试,查找有问题的应用程序等似乎非常困难。
我也是通过将应用程序向后迁移到零来实现此目的,因此我认为它来自于在迁移命令的最后运行的某些内容。
使用我的某个应用程序的迁移文件进行说明;
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
答案 0 :(得分:0)
尝试按应用程序单独运行迁移:
python manage.py migrate <appname>