#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.

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.b4a81ef4e2a1f08b62bbd8cc41e6bf7f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to