#35575: Add support for constraint validation on GeneratedFields
-------------------------------------+-------------------------------------
Reporter: Mark Gensler | Owner: Mark
| Gensler
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: generatedfield | Triage Stage: Accepted
uniqueconstraint checkconstraint |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mark Gensler):
Replying to [comment:7 Sarah Boyce]:
> I also think it's worth highlighting that this has come from us testing
a PR and not "organically". I don't think this is a quick win and we
should probably confirm that this is wanted, otherwise it might be best to
document that it isn't supported.
How would we do that? Shall I raise a discussion on the django forum?
I don't see any gap in pure functionality at present. Any expression which
is defined for a `GeneratedField` could also be written directly as an
input to the `CheckConstraint` / `UniqueConstraint` /
`ExclusionConstraint`, rather than by using the `GeneratedField` as
shorthand for that expression. This change would simply allow a more
concise and readable definition of the constraint and assist with DRY. Or
is there another advantage which I'm missing?
If this isn't required, the only change necessary would be to prevent a
`GeneratedField` appearing in the expression of any constraints. Otherwise
a stale value would be returned for the `GeneratedField` attribute of an
instance during `constraint.validate()`. This wouldn't be strictly
backwards compatible, but users could re-write any existing constraints
which use a `GeneratedField`.
Replying to [comment:6 Simon Charette]:
Thanks for the examples and for raising `ExclusionConstraint`, I had
missed that. I'll investigate a solution using the outline you provided.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35575#comment:8>