From db13bca60a6758d5fe63eeb01c00c3f54f650715 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 5 Aug 2018 22:30:44 -0400 Subject: 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 --- docs/ref/models/constraints.txt | 25 +++++++++++++++++++++++++ docs/releases/2.2.txt | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/ref/models/constraints.txt b/docs/ref/models/constraints.txt index 9e24a1cad8..5c94ce094a 100644 --- a/docs/ref/models/constraints.txt +++ b/docs/ref/models/constraints.txt @@ -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. diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt index ef68c31eab..8a7a75a0bc 100644 --- a/docs/releases/2.2.txt +++ b/docs/releases/2.2.txt @@ -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 ` option. -- cgit v1.3