简体   繁体   English

django fixtures(来自dumpdata)在测试时失败

[英]django fixtures (from dumpdata) failing when testing

I'm trying to dump data from my production server to use as test on my dev server, but I'm getting errors when running "./manage.py test" on the dev server specifing the fixture file created on the prod server. 我正在尝试从我的生产服务器转储数据以用作我的开发服务器上的测试,但是在开发服务器上运行“./manage.py test”时,我发现错误,指定在prod服务器上创建的fixture文件。

Here are the tries I made based on google/stackoverflow search: 以下是我根据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`))')

I also tried removing "'init_command': 'SET storage_engine=INNODB'" from settings.py and still got 1062 errors. 我还尝试从settings.py中删除“'init_command':'SET storage_engine = INNODB'”,但仍然有1062个错误。

I don't understand this problem. 我不明白这个问题。 Shouldn't django just recreate the DB exactly as it was on the prod server when I load the fixtures? 当我加载灯具时,难道django不应该像在prod服务器上那样重新创建数据库吗?

I had similar problems and these args worked for me: 我有类似的问题,这些args为我工作:

python manage.py dumpdata --natural --exclude auth.permission --exclude contenttypes --indent 4

I also had a lot of issues with the post_save signal creating objects twice. 我也有很多问题,post_save信号创建对象两次。 There's a fix for that: How do I prevent fixtures from conflicting with django post_save signal code? 有一个解决方法: 如何防止灯具与django post_save信号代码冲突?

I believe the errors are telling you exactly what is happening. 我相信这些错误正在告诉你究竟发生了什么。 Is app_label unique? app_label独一无二吗? I would guess it is. 我猜是的。 I would think you have two objects with the same app_label key value. 我认为你有两个具有相同app_label键值的对象。 ( cms-agencies ) cms-agencies

Additionally when you have a foreign key relationship you need to have the object that corresponds to the foreign key. 此外,当您具有外键关系时,您需要具有与外键对应的对象。 Dump data doesn't do this because it only dumps one model. 转储数据不会这样做,因为它只转储一个模型。

Something like https://github.com/davedash/django-fixture-magic is great for this. https://github.com/davedash/django-fixture-magic这样的东西很棒。 It dumps your model AND all fk dependencies. 它会转储您的模型和所有fk依赖项。

The problem is, that if you use natural keys (natural-foreign keys in later versions of Django), Django will actually store many-to-many relationships inside parent objects. 问题是,如果你使用自然键(在更高版本的Django中使用自然外键),Django实际上会在父对象中存储多对多关系。 That is something you want. 那是你想要的。 But then you cannot simply dump all tables, you must not include the many-to-many tables/models in your dump as you would then load same data twice - and boom, duplicates and IntegrityErrors. 但是,您不能简单地转储所有表,您不能在转储中包含多对多表/模型,因为您将加载相同的数据两次 - 以及繁荣,重复和IntegrityErrors。

For example, you should dump auth.User and auth.Group , but not auth.User_Groups . 例如,您应该转储auth.Userauth.Group ,但不能转储auth.User_Groups Look at example of a dump from Django 1.7: 看一下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": [],
    },
},

Following line creates a comprehensive dump of users/permissions and content types you can move to dev to get identical copy, including identical row order and primary keys (tested on Django 1.7): 以下行创建了一个全面的用户/权限转储和内容类型,您可以移动到dev以获得相同的副本,包括相同的行顺序和主键(在Django 1.7上测试):

python manage.py dumpdata auth.User auth.Group contenttypes auth.Permission --indent 4 --natural-foreign > users.json

your problem might be that you dumped out a unicode file but django expects a ascii file format when loading. 你的问题可能是你丢弃了一个unicode文件,但django在加载时需要一个ascii文件格式。 This will happen using PowerShell on Windows if that applies to you. 如果适用于您,则会在Windows上使用PowerShell进行此操作。

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

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