Opened 7 years ago

Closed 7 years ago

#7202 closed (fixed)

fixture loading fails for one-to-one relations

Reported by: zhaoz Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Keywords:
Cc: zilingzhao@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

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)

onetoone_fixtures_test_r7526.patch (1.1 KB) - added by zhaoz 7 years ago.
Adds a test for onetoone fixture loading -- (needs the onetoone.json fixture)
onetoone.json (237 bytes) - added by zhaoz 7 years ago.
The fixture to be used with the onetoone test.
onetoone_fixtures_test_r7526.diff (1.1 KB) - added by zhaoz 7 years ago.
Renamed the .patch to .diff
o2o_fixture_load_r7540.diff (680 bytes) - added by zhaoz 7 years ago.
The fix, sets parent's pk so update/save new works correctly.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 years ago by zhaoz

  • Cc zilingzhao@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Changed 7 years ago by zhaoz

Adds a test for onetoone fixture loading -- (needs the onetoone.json fixture)

Changed 7 years ago by zhaoz

The fixture to be used with the onetoone test.

Changed 7 years ago by zhaoz

Renamed the .patch to .diff

Changed 7 years ago by zhaoz

The fix, sets parent's pk so update/save new works correctly.

comment:2 Changed 7 years ago by zhaoz

  • Component changed from Database wrapper to Serialization
  • Has patch set

comment:3 Changed 7 years ago by zhaoz

  • Patch needs improvement set

Patch actually fails for some cases. Will need to look at patch/test case.

comment:4 Changed 7 years ago by zhaoz

  • Has patch unset
  • Patch needs improvement unset

comment:5 Changed 7 years ago by zhaoz

  • 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 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to 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.
Note: See TracTickets for help on using tickets.
Back to Top