Opened 17 years ago
Closed 17 years ago
#7202 closed (fixed)
fixture loading fails for one-to-one relations
Reported by: | zhaoz | Owned by: | nobody |
---|---|---|---|
Component: | Core (Serialization) | Version: | dev |
Severity: | Keywords: | ||
Cc: | zilingzhao@… | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Models created with one-to-one relation as documented fail to produce loadable fixtures.
Fixtures created with dumpdata and then reloaded with loaddata fail with this error:
ERROR: duplicate key violates unique constraint "App_model_pkey"
Tested with dumpdata using json on postgresql.
Also tested on sqlite
Models:
from django.db import models # Create your models here. class Parent(models.Model): name = models.CharField(max_length=60, unique=True) class Child(Parent): parent = models.OneToOneField(Parent, parent_link=True) number = models.IntegerField(max_length=20)
Dumpdata
$ ./manage.py dumpdata --format=json first > first/fixtures/test.json $ cat first/fixtures/test.json [{"pk": 1, "model": "first.parent", "fields": {"name": "first1"}}, {"pk": 1, "model": "first.child", "fields": {"name": "first1", "number": 2}}] $ ./manage.py loaddata test Installing json fixture 'test' from '/home/zhaoz/programming/python/django_test/onetoone/../onetoone/first/fixtures'. Problem installing fixture '/home/zhaoz/programming/python/django_test/onetoone/../onetoone/first/fixtures/test.json': column name is not unique
Attachments (4)
Change History (10)
comment:1 by , 17 years ago
Cc: | added |
---|
by , 17 years ago
Attachment: | onetoone_fixtures_test_r7526.patch added |
---|
by , 17 years ago
Attachment: | o2o_fixture_load_r7540.diff added |
---|
The fix, sets parent's pk so update/save new works correctly.
comment:2 by , 17 years ago
Component: | Database wrapper → Serialization |
---|---|
Has patch: | set |
comment:3 by , 17 years ago
Patch needs improvement: | set |
---|
Patch actually fails for some cases. Will need to look at patch/test case.
comment:4 by , 17 years ago
Has patch: | unset |
---|---|
Patch needs improvement: | unset |
comment:5 by , 17 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
The patch works for every case with the exception of when the parent model and the child model share the same pk field name.
comment:6 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [7600]) Fixed #7350, #7202 -- Fixed serialization for multi-model inheritance, which had multiple problems:
- Serializers were including all superclass fields in their output. Now only local fields are included.
- Implicit OneToOne primary keys were not correctly added to the metamodel, so they were always marked to be serialized, even though they were primary
- Model saving was too aggressive about creating new parent class instances during deserialization. Raw save on a model now skips saving of the parent class.
Adds a test for onetoone fixture loading -- (needs the onetoone.json fixture)