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.
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:1>
* cc: Ian Foote (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:2>
* 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>
* owner: (none) => Ian Foote
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/29641#comment:4>
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>
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>
* 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>