[Django] #34586: create does not save reverse OneToOne relantionship and does not raise an Exception

2 views
Skip to first unread message

Django

unread,
May 21, 2023, 11:25:20 PM5/21/23
to django-...@googlegroups.com
#34586: create does not save reverse OneToOne relantionship and does not raise an
Exception
-------------------------------------+-------------------------------------
Reporter: alarivas | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34586>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 22, 2023, 8:46:05 AM5/22/23
to django-...@googlegroups.com
#34586: create does not save reverse OneToOne relantionship and does not raise an
Exception
-------------------------------------+-------------------------------------
Reporter: Alan Rivas | Owner: stimver
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by stimver):

* owner: nobody => stimver
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34586#comment:1>

Reply all
Reply to author
Forward
0 new messages