#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 stimver, 18 months ago

Owner: changed from nobody to stimver
Status: newassigned

comment:2 by Mariusz Felisiak, 18 months ago

Summary: create does not save reverse OneToOne relantionship and does not raise an Exceptioncreate() does not save reverse OneToOne relationship nor raise an Exception
Triage Stage: UnreviewedAccepted

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).

comment:4 by Mariana, 16 months ago

Owner: set to Mariana

comment:5 by Mariana, 15 months ago

Has patch: set

comment:6 by Mariusz Felisiak, 15 months ago

Patch needs improvement: set

comment:7 by Mariana, 15 months ago

Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 15 months ago

Patch needs improvement: set

comment:9 by Mariusz Felisiak, 15 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

Resolution: fixed
Status: assignedclosed

In e02fc588:

Fixed #34586 -- Made QuerySet.create() raise ValueError for reverse one-to-many relations.

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