[Django] #31583: Extend deferred unique constraint support to OneToOneField

27 views
Skip to first unread message

Django

unread,
May 13, 2020, 10:19:26 PM5/13/20
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: DEFERRED
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Support for DEFERRABLE INITIALLY DEFERRED was added in
https://code.djangoproject.com/ticket/20581 . It would be great if it
could be extended to the implicit Unique constraint generated by
`OneToOneField`, say, by adding an argument:
`OneToOneField(OtherModel, defer=models.Deferrable.DEFERRED)`

--
Ticket URL: <https://code.djangoproject.com/ticket/31583>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 14, 2020, 8:55:43 PM5/14/20
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DEFERRED | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Someday/Maybe


Comment:

Not sure we should do that as it seems pretty niche.

I guess an alternative API could be to allow passing a `UniqueConstraint`
to the `unique` kwarg like we've discussed doing to the `Field.index` one.

e.g.

{{{#!python
OneToOneField(OtherModel,
unique=UniqueConstraint(defer=models.Deferrable.DEFERRED))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31583#comment:1>

Django

unread,
Apr 2, 2021, 7:06:18 PM4/2/21
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: DEFERRED | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ian Foote):

* cc: Ian Foote (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/31583#comment:2>

Django

unread,
Dec 15, 2021, 8:08:15 AM12/15/21
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DEFERRED | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petr Přikryl):

* cc: Petr Přikryl (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/31583#comment:3>

Django

unread,
Dec 16, 2021, 5:28:06 AM12/16/21
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DEFERRED | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Petr Přikryl):

I think it would be quite easy. Here we could pass `deferrable` argument
based on `new_field.defer` value:

https://github.com/django/django/blob/4.0/django/db/backends/base/schema.py#L810
https://github.com/django/django/blob/4.0/django/db/backends/base/schema.py#L1245

--
Ticket URL: <https://code.djangoproject.com/ticket/31583#comment:4>

Django

unread,
Mar 1, 2024, 10:15:00 AMMar 1
to django-...@googlegroups.com
#31583: Extend deferred unique constraint support to OneToOneField
-------------------------------------+-------------------------------------
Reporter: BorisZZZ | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DEFERRED | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jefflieb-wavy):

There is a use case for this, ordering of database rows using a linked
list. OneToOne can be used to define the field pointing to the previous
row, but the associated constraint needs to be deferred to allow for
updates that temporarily violate the constraint while changing the row
ordering.

We were able to implement this, but it involved a workaround.
Specifically we had to manually delete the constraint created by the
OneToOne and then recreate it with deferred in our migrations, then add
that constraint to the model definition also.
--
Ticket URL: <https://code.djangoproject.com/ticket/31583#comment:5>
Reply all
Reply to author
Forward
0 new messages