#33137: Decouple Field.unique from select_related
-------------------------------------+-------------------------------------
Reporter: Markus | Owner: nobody
Holtermann |
Type: New | Status: new
feature |
Component: Database | Version:
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When inheriting from a `OneToOneField` that automatically adds additional
constraints to a model, the object-level relation might be a one-to-one
relation, while the underlying implementation is still a many-to-one.
Let's say you have two models `A` and `B` where `A.rel =
MyOneToOneField(B)` and `A.deleted = BooleanField()`. In a regular
`OneToOneField` there would now be a unique index on `A.rel`. However, by
adding a unique constraint to `A._meta.constraints` over `rel` with a
condition on `deleted=False`, `rel` only needs to be unique among the
"undeleted" objects. Doing so is possible by forcing
`MyOneToOneField.unique=False` (otherwise migrations create a constraint).
However, this will mean, `SQLCompiler.get_related_selections()` fails when
trying to do `B.objects.select_related("a")`, since `a` is not a valid
field. That is because
`SQLCompiler.get_related_selections._get_field_choices()` uses
`f.field.unique` instead of `(f.field.many_to_one or f.field.one_to_one)`,
I think. Because, essentially, `opts.related_objects` is build based on
those `(many|one)_to_(many|one)` field attributes.
--
Ticket URL: <https://code.djangoproject.com/ticket/33137>
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/050.34274cc4a776fec0de16c7d36777d50e%40djangoproject.com.