#37499 assigned Cleanup/optimization

Error messages say "unsaved" when the check is whether the primary key is set

Reported by: Alin Morosanu Owned by: Alin Morosanu
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no 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 (0)

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