#35575: Model.full_clean() does not recalculate GeneratedFields prior to
validating
model constraints
-------------------------------------+-------------------------------------
Reporter: Mark Gensler | Owner: Mark
| Gensler
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage:
uniqueconstraint checkconstraint | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Mark Gensler:
Old description:
> `GeneratedField`s on a model instance are not recalculated during
> `Model.full_clean()`. Therefore, if a `GeneratedField` is included in the
> `*expressions` or `fields` parameters of a `UniqueConstraint` or
> `CheckConstraint` in the instance's `Model._meta`, the new values of the
> `GeneratedField` attribute are not uaed by `BaseConstraint.validate()`.
>
> Instead, the model instance's `GeneratedField` attribute will return the
> value which corresponds to the most recent refresh of the instance from
> the database. If the instance is new and has not yet been saved, the
> following error is raised:
>
> `AttributeError: Cannot read a generated field from an unsaved model.`
>
> An `IntegrityError` is correctly raised when calling `Model.save()` if
> any constraints involving `GeneratedField`s are violated.
>
> Instead, the recalculated values for `GeneratedField`s should be used
> when checking constraints.
>
> This ticket was raised after fixing the bug in #35560 with PR
> [https://github.com/django/django/pull/18309].
New description:
`GeneratedField`s on a model instance are not recalculated during
`Model.full_clean()`. Therefore, if a `GeneratedField` is included in the
`*expressions` or `fields` parameters of a `UniqueConstraint` or
`CheckConstraint` in the instance's `Model._meta`, the new values of the
`GeneratedField` attribute are not used by `BaseConstraint.validate()`.
Instead, the model instance's `GeneratedField` attribute will return the
value which corresponds to the most recent refresh of the instance from
the database. If the instance is new and has not yet been saved, the
following error is raised:
`AttributeError: Cannot read a generated field from an unsaved model.`
An `IntegrityError` is correctly raised when calling `Model.save()` if any
constraints involving `GeneratedField`s are violated.
Instead, the recalculated values for `GeneratedField`s should be used when
checking constraints.
This ticket was raised after fixing the bug in #35560 with PR
[https://github.com/django/django/pull/18309].
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35575#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070190785e5a71-58029b48-c9c3-4dfa-b02d-73cfc68cf746-000000%40eu-central-1.amazonses.com.