我试图从我的生产服务器转储数据,以便在我的开发服务器上用作测试,但在开发服务器上运行"./manage.py test“指定在prod服务器上创建的fixture文件时出现错误。
以下是我基于google/stackoverflow搜索所做的尝试:
# python manage.py dumpdata --indent=4 --natural
error when running tests: IntegrityError: (1062, "Duplicate entry 'cms-agencies' for key 'app_label'")
# python manage.py dumpdata --exclude contenttypes --indent=4
error when running tests: IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_current`.`django_admin_log`, CONSTRAINT `content_type_id_refs_id_288599e6` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`))')
# python manage.py dumpdata --exclude contenttypes --natural --indent=4
error when running tests: IntegrityError: (1062, "Duplicate entry '14-add_agencies' for key 'content_type_id'")
# python manage.py dumpdata --exclude contenttypes --exclude auth --indent=4
error when running tests: IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_current`.`django_admin_log`, CONSTRAINT `user_id_refs_id_c8665aa` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')
# python manage.py dumpdata --exclude contenttypes --exclude auth --natural --indent=4
error when running tests: IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_current_abril`.`django_admin_log`, CONSTRAINT `user_id_refs_id_c8665aa` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')我还尝试从settings.py中删除"'init_command':'SET storage_engine=INNODB'“,但仍然有1062个错误。
我不明白这个问题。django难道不应该完全按照我加载fixture时在prod服务器上的样子重新创建DB吗?
发布于 2012-10-04 13:20:00
我也遇到过类似的问题,下面这些参数对我很有效:
python manage.py dumpdata --natural --exclude auth.permission --exclude contenttypes --indent 4我在两次创建对象的post_save信号上也遇到了很多问题。有一个解决方案:How do I prevent fixtures from conflicting with django post_save signal code?
发布于 2012-04-27 20:49:26
我相信这些错误会告诉你到底发生了什么。app_label是独一无二的吗?我想是的。我认为有两个对象具有相同的app_label键值。(cms-agencies)
此外,当您具有外键关系时,您需要拥有与外键相对应的对象。转储数据不会这样做,因为它只转储一个模型。
像https://github.com/davedash/django-fixture-magic这样的东西对此很有帮助。它会转储模型和所有fk依存关系。
发布于 2016-04-01 07:06:15
问题是,如果使用自然键( Django后续版本中的自然外键),Django实际上会在父对象中存储多对多关系。这是你想要的。但是你不能简单地转储所有的表,你不能在你的转储中包含多对多的表/模型,因为你会加载相同的数据两次-然后轰隆,重复和IntegrityErrors。
例如,您应该转储auth.User和auth.Group,但不能转储auth.User_Groups。看看Django 1.7中的转储示例:
{
"model": "auth.group",
"fields": {
"permissions": [],
"name": "ST"
},
},
{
"model": "auth.group",
"fields": {
"permissions": [],
"name": "property_manager"
},
},
{
"model": "auth.user",
"fields": {
"username": "boss",
"groups": [
["property_manager"],["ST"],
],
"user_permissions": [],
},
},下面这行代码创建了一个全面的用户/权限和内容类型的转储,您可以将其移动到dev以获得相同的副本,包括相同的行顺序和主键(在Django 1.7上测试):
python manage.py dumpdata auth.User auth.Group contenttypes auth.Permission --indent 4 --natural-foreign > users.jsonhttps://stackoverflow.com/questions/10350559
复制相似问题