Fixed #20581 -- Added support for deferrable unique constraints.

This commit is contained in:
Ian Foote 2018-08-27 03:25:06 +01:00 committed by Mariusz Felisiak
parent 555e3a848e
commit c226c6cb32
14 changed files with 457 additions and 16 deletions

View file

@ -354,6 +354,8 @@ Models
* **models.W036**: ``<database>`` does not support unique constraints with
conditions.
* **models.W037**: ``<database>`` does not support indexes with conditions.
* **models.W038**: ``<database>`` does not support deferrable unique
constraints.
Security
--------

View file

@ -76,7 +76,7 @@ The name of the constraint.
``UniqueConstraint``
====================
.. class:: UniqueConstraint(*, fields, name, condition=None)
.. class:: UniqueConstraint(*, fields, name, condition=None, deferrable=None)
Creates a unique constraint in the database.
@ -119,3 +119,35 @@ ensures that each user only has one draft.
These conditions have the same database restrictions as
:attr:`Index.condition`.
``deferrable``
--------------
.. attribute:: UniqueConstraint.deferrable
.. versionadded:: 3.1
Set this parameter to create a deferrable unique constraint. Accepted values
are ``Deferrable.DEFERRED`` or ``Deferrable.IMMEDIATE``. For example::
from django.db.models import Deferrable, UniqueConstraint
UniqueConstraint(
name='unique_order',
fields=['order'],
deferrable=Deferrable.DEFERRED,
)
By default constraints are not deferred. A deferred constraint will not be
enforced until the end of the transaction. An immediate constraint will be
enforced immediately after every command.
.. admonition:: MySQL, MariaDB, and SQLite.
Deferrable unique constraints are ignored on MySQL, MariaDB, and SQLite as
neither supports them.
.. warning::
Deferred unique constraints may lead to a `performance penalty
<https://www.postgresql.org/docs/current/sql-createtable.html#id-1.9.3.85.9.4>`_.

View file

@ -381,6 +381,9 @@ Models
<sqlite3.Connection.create_function>` on Python 3.8+. This allows using them
in check constraints and partial indexes.
* The new :attr:`.UniqueConstraint.deferrable` attribute allows creating
deferrable unique constraints.
Pagination
~~~~~~~~~~