mirror of
https://github.com/django/django.git
synced 2025-07-24 05:36:15 +00:00
Fixed #20581 -- Added support for deferrable unique constraints.
This commit is contained in:
parent
555e3a848e
commit
c226c6cb32
14 changed files with 457 additions and 16 deletions
|
@ -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
|
||||
--------
|
||||
|
|
|
@ -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>`_.
|
||||
|
|
|
@ -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
|
||||
~~~~~~~~~~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue