【问题标题】:django fixtures (from dumpdata) failing when testingdjango 固定装置(来自转储数据)在测试时失败
【发布时间】:2012-05-08 04:44:19
【问题描述】:

我正在尝试从生产服务器转储数据以用作我的开发服务器上的测试,但在指定在开发服务器上创建的夹具文件的开发服务器上运行“./manage.py test”时出现错误产品服务器。

以下是我根据 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 不应该完全按照产品服务器上的方式重新创建数据库吗?

【问题讨论】:

  • 同意,希望可以选择只复制数据库

标签: django


【解决方案1】:

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

例如,您应该转储 auth.Userauth.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.json

【讨论】:

    【解决方案2】:

    您的问题可能是您转储了一个 unicode 文件,但 django 在加载时需要一个 ascii 文件格式。如果这适用于您,这将在 Windows 上使用 PowerShell 发生。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,这些参数对我有用:

      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?

      【讨论】:

      • 谢谢! --natural 已在 Django 1.9 中弃用,替换为 --natural-foreign。详情请见the documentation
      【解决方案4】:

      我相信这些错误可以准确地告诉您正在发生的事情。 app_label 是唯一的吗?我猜它是。我认为您有两个具有相同 app_label 键值的对象。 (cms-agencies)

      此外,当您有外键关系时,您需要拥有与外键对应的对象。 转储数据不会这样做,因为它只转储一个模型。

      类似的东西 https://github.com/davedash/django-fixture-magic 非常适合这个。它会转储您的模型和所有 fk 依赖项。

      【讨论】:

      • 在夹具中,是的。我猜正在发生的是 django 将一些数据添加到数据库(它自己的模型),然后它与夹具冲突
      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2012-04-20
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多