Opened 18 months ago
Closed 15 months ago
#34586 closed Bug (fixed)
create() does not save reverse OneToOne relationship nor raise an Exception
Reported by: | Alan Rivas | Owned by: | Mariana |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When creating an object with a reverse OneToOne relationship as an argument, the method Model.create does not set the relation but does not raise an Error either.
Example:
models:
from django.db import models class Place(models.Model): pass class Restaurant(models.Model): place = models.OneToOneField( Place, on_delete=models.CASCADE, null=True, )
With these models if you try this:
place = Place.objects.create() restaurant = Restaurant.objects.create() place.restaurant = restaurant place.save(update_fields=["restaurant"])
You will get this error:
ValueError: The following fields do not exist in this model, are m2m fields, or are non-concrete fields: restaurant
But if you try with this code:
restaurant = Restaurant.objects.create() place = Place.objects.create(restaurant=restaurant)
You will not get an Exception and restaurant will not be related with place either. I do understand you can't save in a reverse OneToOne relation but I think this behavior is inconsistent because of the missing Exception.
Change History (9)
comment:1 by , 18 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 18 months ago
Summary: | create does not save reverse OneToOne relantionship and does not raise an Exception → create() does not save reverse OneToOne relationship nor raise an Exception |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 16 months ago
Owner: | set to |
---|
comment:6 by , 15 months ago
Patch needs improvement: | set |
---|
comment:7 by , 15 months ago
Patch needs improvement: | unset |
---|
comment:8 by , 15 months ago
Patch needs improvement: | set |
---|
comment:9 by , 15 months ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Tentatively accepted. This is a side-effect of 8a47ba679d2da0dee74671a53ba0cd918b433e34 (see also #16508 and #18599). As far as I'm aware, we could raise an exception as we are not going to support it (see #18638).