加载 Django 固定装置时出现问题:IntegrityError:(1062,“密钥‘user_id’的重复条目‘4’”)
我使用以下命令生成 2 个固定装置:
./manage.py dumpdata --format=json --indent=4 --natural auth.User > fixtures/user.json
./manage.py dumpdata --format=json --indent=4 --natural --exclude=contenttypes --exclude=auth > fixtures/full.json
我有以下名为 user.json 的固定装置:
[
{
"pk": 4,
"model": "auth.user",
"fields": {
"username": "foo",
"first_name": "Se\u00e1n",
"last_name": "Hayes",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2010-09-27 21:57:45",
"groups": [],
"user_permissions": [],
"password": "!",
"email": "[email protected]",
"date_joined": "2010-09-27 21:57:45"
}
}
]
以及以下名为 full.json 的固定装置:
[
{
"pk": "72a75887b4a0ce06a61f9183fe1c0e15",
"model": "sessions.session",
"fields": {
"expire_date": "2010-10-11 21:57:45",
"session_data": "gAJ9cQEoVRJfYXV0aF91c2VyX2JhY2tlbmRxAlUOZmIuYXV0aC5GYkF1dGhxA1UNX2F1dGhfdXNl\ncl9pZHEEigEEdS5hOGZlODU0MmRjYmUwNmEzODIwNjhiYzYyODc2MWQxZA==\n"
}
},
{
"pk": 1,
"model": "sites.site",
"fields": {
"domain": "example.com",
"name": "example.com"
}
},
{
"pk": 2,
"model": "common.userprofile",
"fields": {
"money": 10,
"energy": 10,
"experience": 0,
"stamina": 10,
"health": 10,
"user": 4
}
},
{
"pk": 2,
"model": "missions.missionprofile",
"fields": {
"user": 4,
"last_area_viewed": null
}
},
{
"pk": 1,
"model": "fb.facebookuser",
"fields": {
"updated": "2010-09-27 21:57:45",
"uid": "24411841",
"created": "2010-09-27 21:57:45",
"access_token": "foo",
"url": "http://www.facebook.com/profile.php?id=24411841",
"user": 4,
"img_url": null,
"name": "Se\u00e1n Hayes"
}
}
]
运行以下命令(以任一顺序):
./manage.py loaddata user
./manage.py loaddata full
引发以下异常:
Problem installing fixture '/projectpath/fixtures/full.json': Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/core/management/commands/loaddata.py", line 169, in handle
obj.save(using=using)
File "/usr/lib/pymodules/python2.6/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 528, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1479, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 727, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '4' for key 'user_id'")
关于什么的任何想法发生什么事了?我知道 MySQL 中的前向引用存在问题,但是如果 id 为 4 的 User 对象在包含外键的装置之前安装,那应该不重要,对吧?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Ashok 的评论,当我遇到同样的问题时,通过更改信号处理程序以检查它是否在“原始”模式下运行来解决,这显然意味着正在加载固定装置:
请参阅 如何防止固定装置与 django post_save 信号代码发生冲突?< /a>
As per Ashok's comment, when I had the same problem, it was solved by changing my signal handler to check for whether it is running in "raw" mode which apparently means a fixture is being loaded:
See How do I prevent fixtures from conflicting with django post_save signal code?
Django 的静态装置是一种反模式,这就是您遇到此类问题的原因。我建议您使用一些为您生成这些装置的库,自然包括 ID。
我建议你Dynamic动态夹具
Static fixture of Django is an ANTI-PATTERN, that is the reason you are having this kind of problem. I suggest you to use some library that generate these fixtures for you, including the ID naturally.
I suggest you Dynamic Dynamic Fixture