#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)
Change History (9)
by , 12 years ago
Attachment: | 18638-test.patch added |
---|
comment:1 by , 12 years ago
Patch needs improvement: | set |
---|
comment:2 by , 12 years ago
Owner: | changed from | to
---|
comment:3 by , 12 years ago
Cc: | added |
---|
comment:4 by , 12 years ago
Owner: | changed from | to
---|
follow-up: 8 comment:5 by , 12 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
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 , 4 years ago
Has patch: | unset |
---|---|
Patch needs improvement: | unset |
Resolution: | worksforme → wontfix |
Type: | Bug → New feature |
#32056 was closed as a duplicate, we can treat this as a request for a new feature.
comment:8 by , 19 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.
test case