Opened 12 years ago

Closed 12 years ago

Last modified 11 months ago

#18638 closed New feature (wontfix)

Reverse OneToOne relationship does not save properly.

Reported by: David Hatch Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: David Hatch Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When the reverse side of a one-to-one relationship is assigned to and the model is saved, the association is not properly written to the database.

Example:

Models:

class Tag(models.Model):
    data = models.OneToOneField("Data")

class Data(models.Model):
    pass

Run:

data = Data()
t = Tag.objects.create()
data.tag = t
data.save()
# refresh from db
data = Data.objects.get(pk=data.pk)
data.tag # raises DoesNotExist

Attached is a patch including a test case which illustrates this issue.
To run the test case cd to the tests directory then...

export PYTHONPATH=`pwd`:`pwd`/..; ./runtests.py --settings=test_sqlite one_to_one_regress

test_reverse_one_to_one_save should fail.

Attachments (1)

18638-test.patch (1.1 KB ) - added by David Hatch 12 years ago.
test case

Download all attachments as: .zip

Change History (9)

by David Hatch, 12 years ago

Attachment: 18638-test.patch added

test case

comment:1 by anonymous, 12 years ago

Patch needs improvement: set

comment:2 by David Hatch, 12 years ago

Owner: changed from nobody to David Hatch

comment:3 by David Hatch, 12 years ago

Cc: David Hatch added

comment:4 by David Hatch, 12 years ago

Owner: changed from David Hatch to nobody

comment:5 by Aymeric Augustin, 12 years ago

Resolution: worksforme
Status: newclosed

You're seeing the expected behavior. You'd need to call tag.save().

The API for 1-to-1 relationships isn't totally symmetrical -- after all it isn't at the SQL level.

comment:6 by Mariusz Felisiak, 3 years ago

Has patch: unset
Patch needs improvement: unset
Resolution: worksformewontfix
Type: BugNew feature

#32056 was closed as a duplicate, we can treat this as a request for a new feature.

comment:7 by Mariusz Felisiak, 12 months ago

#34433 was closed as a duplicate.

in reply to:  5 comment:8 by NKSM, 11 months ago

Replying to Aymeric Augustin:

You're seeing the expected behavior. You'd need to call tag.save().

The API for 1-to-1 relationships isn't totally symmetrical -- after all it isn't at the SQL level.

This should be documented here:

https://docs.djangoproject.com/en/dev/topics/db/examples/one_to_one/.

The way it is now described is confusing.

Note: See TracTickets for help on using tickets.
Back to Top