Opened 3 weeks ago

Closed 2 weeks ago

#37499 closed Cleanup/optimization (fixed)

Clarify QuerySet.contains() error for objects without a primary key

Reported by: Alin Morosanu Owned by: Alin Morosanu
Component: Database layer (models, ORM) Version: dev
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

Follow-up to #36259, see ticket:36259#comment:19.

Several ORM errors call an object "unsaved", but the code only checks whether its primary key is set (_is_pk_set()). For most models these are the same. They differ when the primary key is set by hand or by a default. In comment:19, Jacob thought this wording may have led the earlier work on #36259 towards _state.adding.

Messages that only check the primary key:

  • save() prohibited to prevent data loss due to unsaved related object '...'.
  • Model instances passed to related filters must be saved.
  • QuerySet.contains() cannot be used on unsaved objects.
  • get_next/get_previous cannot be used on unsaved objects.
  • Unsaved model instance ... cannot be used in an ORM query.
  • Cannot retrieve deferred field '...' from an unsaved model.

Related managers already use clearer wording: ... instance needs to have a primary key value before this relationship can be used.

Proposal: reword these to talk about the primary key, for example QuerySet.contains() cannot be used on objects without a primary key. Only the text changes, not the behavior. Tests that check the old text would be updated.

Is this worth doing, and should all of these change or only some?

Change History (6)

comment:1 by ontowhee, 3 weeks ago

Triage Stage: Unreviewed → Accepted

Hi Alin, thank you so much for looking into this problem, and for joining the triage session!

Raffaella and I reviewed this with you, and from our discussion, we think most of the error messages should still use the phrase "unsaved objects". For Model instances passed to related filters must be saved., there was ticket 31486, which specifically made a change to prevent unsaved objects from being used. Similarly for the others, they are expecting there to be existing rows in the database table, so it is required to have saved objects in those cases.

There is one message that could be enhanced by changing the error message to refer more explicitly to the primary key, under the condition that it is acceptable to pass in unsaved objects. This is the error message, QuerySet.contains() cannot be used on unsaved objects.

To put things in perspective, an equivalent query such as Place.objects.filter(pk=999).exists(), where a row with pk=999 does not exist will return False. A query filtering on pk=None will also return False.

However, there is a different behavior when using the QuerySet.contains() method. With pk=None, the error message gets raised. With pk=999 the error message doesn't get raised. This behavior is consistent with the GeneratedField behavior as described in comment 18 and comment 19 of ticket 36259, where the query is allowed to happen if a pk is set (regardless of whether that pk actually exists or not in the database), and an error message is raised by Django if a pk is not set.

With that, changing the error message to what you suggested, "QuerySet.contains() cannot be used on objects without a primary key." will be helpful to the user.

comment:2 by Alin Morosanu, 3 weeks ago

Summary: Error messages say "unsaved" when the check is whether the primary key is set → Clarify QuerySet.contains() error for objects without a primary key

comment:3 by Alin Morosanu, 3 weeks ago

Thanks Lilian, and thanks Raffaella, for going through this with me in the triage session. I've updated the ticket title to match, and I'll prepare a PR with the agreed wording

comment:5 by Jacob Walls, 2 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:6 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

Resolution: → fixed
Status: assigned → closed

In 9e2e5a31:

Fixed #37499 -- Clarified QuerySet.contains() error for unset primary keys.

QuerySet.contains() checks whether the object's primary key is set, not
whether the object was saved. The error message now says so.

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