Problems with contenttypes when loading a fixture in Django(在 Django 中加载夹具时的内容类型问题)
问题描述
由于内容类型冲突,我无法将 Django 固定装置加载到我的 MySQL 数据库中.首先,我尝试仅从我的应用程序中转储数据,如下所示:
I am having trouble loading Django fixtures into my MySQL database because of contenttypes conflicts. First I tried dumping the data from only my app like this:
./manage.py dumpdata escola > fixture.json
但我一直缺少外键问题,因为我的应用程序escola"使用来自其他应用程序的表.我一直在添加其他应用程序,直到我做到这一点:
but I kept getting missing foreign key problems, because my app "escola" uses tables from other applications. I kept adding additional apps until I got to this:
./manage.py dumpdata contenttypes auth escola > fixture.json
现在,当我尝试将数据作为测试装置加载时,问题是以下约束冲突:
Now the problem is the following constraint violation when I try to load the data as a test fixture:
IntegrityError: (1062, "Duplicate entry 'escola-t23aluno' for key 2")
问题似乎在于 Django 试图动态地重新创建具有与夹具中的主键值冲突的不同主键值的内容类型.这似乎与此处记录的错误相同:http://code.djangoproject.com/ticket/7052
It seems the problem is that Django is trying to dynamically recreate contenttypes with different primary key values that conflict with the primary key values from the fixture. This appears to be the same as bug documented here: http://code.djangoproject.com/ticket/7052
问题是推荐的解决方法是转储我已经在做的 contenttypes 应用程序!?是什么赋予了?如果它有任何不同,我确实有一些自定义模型权限,如此处所述:http://docs.djangoproject.com/en/dev/ref/models/options/#permissions
The problem is that the recommended workaround is to dump the contenttypes app which I'm already doing!? What gives? If it makes any difference I do have some custom model permissions as documented here: http://docs.djangoproject.com/en/dev/ref/models/options/#permissions
推荐答案
manage.py dumpdata --natural
将使用更持久的外键表示.在 Django 中,它们被称为自然键".例如:
manage.py dumpdata --natural
will use a more durable representation of foreign keys. In django they are called "natural keys". For example:
Permission.codename
用于支持Permission.id
User.username
用于支持User.id
Permission.codename
is used in favour ofPermission.id
User.username
is used in favour ofUser.id
阅读更多:序列化 django 对象"中的自然键部分
dumpdata
的一些其他有用参数:
Some other useful arguments for dumpdata
:
--indent=4
使其易于阅读.-e sessions
排除会话数据-e admin
排除管理站点上管理操作的历史-e contenttypes -e auth.Permission
排除在syncdb
期间每次从架构中自动重新创建的对象.仅将它与--natural
一起使用,否则您最终可能会得到严重对齐的 ID 号码.
--indent=4
make it human readable.-e sessions
exclude session data-e admin
exclude history of admin actions on admin site-e contenttypes -e auth.Permission
exclude objects which are recreated automatically from schema every time duringsyncdb
. Only use it together with--natural
or else you might end up with badly aligned id numbers.
这篇关于在 Django 中加载夹具时的内容类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Django 中加载夹具时的内容类型问题
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01