[Django] #29641: Add support for unique constraints to Meta.constraints

11 views
Skip to first unread message

Django

unread,
Aug 5, 2018, 11:43:13 PM8/5/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon | Owner: Simon Charette
Charette |
Type: New | Status: assigned
feature |
Component: Database | Version: master
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 |
-------------------------------------+-------------------------------------
Just like `Meta.indexes[Index]` allows more granularity over
`Field.db_index=True`/ `Meta.index_together` we should make
`Meta.constraints[UniqueConstraint]` an analog for
`Field.unique=True`/`Meta.unique_together`.

This ticket proposes to introduce
`django.db.constraint.UniqueConstraint(fields, name)` to allow such
constraints to be defined with the sole extra feature of allowing a `name`
to be specified.

The mid-term goal of this feature addition is to rely on the partial
indices work (#29547) that is likely to land in 2.2 to allow partial
unique constraints to be defined which is a really useful feature.


e.g.

{{{#!python
class Tweet(models.Model):
user = models.ForeignKey(User, models.CASCADE)
pinned = models.BooleanField(default=False)

class Meta:
constraints = [
UniqueConstraint(
fields=['user', 'pinned'],
condition=Q(pinned=True),
name='pinned_tweet'
),
]
}}}

Adding support for partial constraints should be tracked in a future
ticket as this one will focus on refactoring the `CheckConstraint` work
added for #11964 to the definition of different type of constraints.

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

Django

unread,
Aug 6, 2018, 4:59:53 AM8/6/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

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

* stage: Unreviewed => Accepted


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

Django

unread,
Aug 26, 2018, 6:59:34 PM8/26/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
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/29641#comment:2>

Django

unread,
Aug 29, 2018, 12:52:13 AM8/29/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: (none)
Type: New feature | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* status: assigned => new
* owner: Simon Charette => (none)
* has_patch: 0 => 1


Comment:

Ian started adjusting my initial PR in
https://github.com/django/django/pull/10337.

I'd be great to get the commits referencing #29641 in before the 2.2
release as they are mostly adjustments that will be hard to perform if we
commit to the currently documented interface.

I'd review it but since I've written a large chunk of this code it's a bit
hard for me to do it.

I'm deassigning the ticket given Ian's work so far, feel free to claim the
ticket Ian.

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

Django

unread,
Sep 2, 2018, 6:58:14 AM9/2/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Ian Foote

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* owner: (none) => Ian Foote
* status: new => assigned


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

Django

unread,
Oct 2, 2018, 1:28:43 PM10/2/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

Comment (by Tim Graham <timograham@…>):

In [changeset:"24dc7d89402d533474193fda9b1b999d00e9fb4f" 24dc7d8]:
{{{
#!CommitTicketReference repository=""
revision="24dc7d89402d533474193fda9b1b999d00e9fb4f"
Refs #29641 -- Extracted reusable CheckConstraint logic into a base class.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:5>

Django

unread,
Nov 13, 2018, 4:11:26 PM11/13/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

Comment (by Tim Graham <timograham@…>):

In [changeset:"dba4a634ba999bf376caee193b3378bc0b730bd4" dba4a63]:
{{{
#!CommitTicketReference repository=""
revision="dba4a634ba999bf376caee193b3378bc0b730bd4"
Refs #29641 -- Refactored database schema constraint creation.

Added a test for constraint names in the database.

Updated SQLite introspection to use sqlparse to allow reading the
constraint name for table check and unique constraints.

Co-authored-by: Ian Foote <pyt...@ian.feete.org>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:6>

Django

unread,
Nov 13, 2018, 6:15:22 PM11/13/18
to django-...@googlegroups.com
#29641: Add support for unique constraints to Meta.constraints
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Ian Foote
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"db13bca60a6758d5fe63eeb01c00c3f54f650715" db13bca6]:
{{{
#!CommitTicketReference repository=""
revision="db13bca60a6758d5fe63eeb01c00c3f54f650715"
Fixed #29641 -- Added support for unique constraints in Meta.constraints.

This constraint is similar to Meta.unique_together but also allows
specifying a name.

Co-authored-by: Ian Foote <pyt...@ian.feete.org>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:7>

Reply all
Reply to author
Forward
0 new messages