1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

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 <python@ian.feete.org>
This commit is contained in:
Simon Charette
2018-08-05 22:30:44 -04:00
committed by Tim Graham
parent 8eae094638
commit db13bca60a
7 changed files with 126 additions and 5 deletions

View File

@@ -43,3 +43,28 @@ ensures the age field is never less than 18.
.. attribute:: CheckConstraint.name
The name of the constraint.
``UniqueConstraint``
====================
.. class:: UniqueConstraint(*, fields, name)
Creates a unique constraint in the database.
``fields``
----------
.. attribute:: UniqueConstraint.fields
A list of field names that specifies the unique set of columns you want the
constraint to enforce.
For example ``UniqueConstraint(fields=['room', 'date'], name='unique_location')``
ensures only one location can exist for each ``date``.
``name``
--------
.. attribute:: UniqueConstraint.name
The name of the constraint.

View File

@@ -33,7 +33,8 @@ What's new in Django 2.2
Constraints
-----------
The new :class:`~django.db.models.CheckConstraint` class enables adding custom
The new :class:`~django.db.models.CheckConstraint` and
:class:`~django.db.models.UniqueConstraint` classes enable adding custom
database constraints. Constraints are added to models using the
:attr:`Meta.constraints <django.db.models.Options.constraints>` option.