当为OneToOneField设置为主键的模型加载fixture时,Django无法设置父ID。 models.py:
from django.db import models
class Parent(models.Model):
base_id = models.AutoField(primary_key=True)
class Child(Parent):
base = models.OneToOneField(Parent,
parent_link=True,
primary_key=True,
related_name="child")
夹具:
[
{
"pk": 1,
"model": "fiddle.parent",
"fields": {}
},
{
"pk": 2,
"model": "fiddle.parent",
"fields": {}
},
{
"pk": 3,
"model": "fiddle.parent",
"fields": {}
},
{
"pk": 1,
"model": "fiddle.child",
"fields": {}
},
{
"pk": 2,
"model": "fiddle.child",
"fields": {}
},
{
"pk": 3,
"model": "fiddle.child",
"fields": {}
}
]
回溯:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 55, in handle
self.loaddata(fixture_labels)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 84, in loaddata
self.load_label(fixture_label)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 140, in load_label
obj.save(using=self.using)
File "/tmp/pkTest/lib/python2.7/site-packages/django/core/serializers/base.py", line 164, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/query.py", line 1511, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
cursor.execute(sql, params)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/tmp/pkTest/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: Problem installing fixture '/tmp/pkTest/testPK/./db.json': Could not load fiddle.Child(pk=None): null value in column "base_id" violates not-null constraint
DETAIL: Failing row contains (null).
我使用PostgreSQL作为数据库。任何想法如何解决这个问题?这是一个django bug吗?找不到有类似问题的人:(