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

时间:2022-11-19 10:27:09

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/* search:

以下是我根据google / *搜索进行的尝试:

# 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服务器那样重新创建数据库吗?

4 个解决方案

#1


12  

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. There's a fix for that: How do I prevent fixtures from conflicting with django post_save signal code?

我也有很多问题,post_save信号创建对象两次。有一个解决方法:如何防止灯具与django post_save信号代码冲突?

#2


1  

I believe the errors are telling you exactly what is happening. Is app_label unique? I would guess it is. I would think you have two objects with the same app_label key value. (cms-agencies)

我相信这些错误正在告诉你究竟发生了什么。 app_label独一无二吗?我猜是的。我认为你有两个具有相同app_label键值的对象。 (CMS-机构)

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. It dumps your model AND all fk dependencies.

像https://github.com/davedash/django-fixture-magic这样的东西很棒。它会转储您的模型和所有fk依赖项。

#3


1  

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. 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.

问题是,如果你使用自然键(在更高版本的Django中使用自然外键),Django实际上会在父对象中存储多对多关系。那是你想要的。但是,您不能简单地转储所有表,您不能在转储中包含多对多表/模型,因为您将加载相同的数据两次 - 以及繁荣,重复和IntegrityErrors。

For example, you should dump auth.User and auth.Group, but not auth.User_Groups. Look at example of a dump from Django 1.7:

例如,您应该转储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": [],
    },
},

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

#4


0  

your problem might be that you dumped out a unicode file but django expects a ascii file format when loading. This will happen using PowerShell on Windows if that applies to you.

你的问题可能是你丢弃了一个unicode文件,但django在加载时需要一个ascii文件格式。如果适用于您,则会在Windows上使用PowerShell进行此操作。

#1


12  

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. There's a fix for that: How do I prevent fixtures from conflicting with django post_save signal code?

我也有很多问题,post_save信号创建对象两次。有一个解决方法:如何防止灯具与django post_save信号代码冲突?

#2


1  

I believe the errors are telling you exactly what is happening. Is app_label unique? I would guess it is. I would think you have two objects with the same app_label key value. (cms-agencies)

我相信这些错误正在告诉你究竟发生了什么。 app_label独一无二吗?我猜是的。我认为你有两个具有相同app_label键值的对象。 (CMS-机构)

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. It dumps your model AND all fk dependencies.

像https://github.com/davedash/django-fixture-magic这样的东西很棒。它会转储您的模型和所有fk依赖项。

#3


1  

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. 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.

问题是,如果你使用自然键(在更高版本的Django中使用自然外键),Django实际上会在父对象中存储多对多关系。那是你想要的。但是,您不能简单地转储所有表,您不能在转储中包含多对多表/模型,因为您将加载相同的数据两次 - 以及繁荣,重复和IntegrityErrors。

For example, you should dump auth.User and auth.Group, but not auth.User_Groups. Look at example of a dump from Django 1.7:

例如,您应该转储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": [],
    },
},

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

#4


0  

your problem might be that you dumped out a unicode file but django expects a ascii file format when loading. This will happen using PowerShell on Windows if that applies to you.

你的问题可能是你丢弃了一个unicode文件,但django在加载时需要一个ascii文件格式。如果适用于您,则会在Windows上使用PowerShell进行此操作。