Fixed #33342 -- Added support for using OpClass() in exclusion constraints.

This commit is contained in:
Hannes Ljungberg 2021-11-03 22:21:50 +01:00 committed by Mariusz Felisiak
parent a0d43a7a6e
commit 0e656c02fe
5 changed files with 114 additions and 28 deletions

View file

@ -53,6 +53,10 @@ operators with strings. For example::
Only commutative operators can be used in exclusion constraints.
.. versionchanged:: 4.1
Support for the ``OpClass()`` expression was added.
``index_type``
--------------
@ -143,6 +147,20 @@ For example::
creates an exclusion constraint on ``circle`` using ``circle_ops``.
Alternatively, you can use
:class:`OpClass() <django.contrib.postgres.indexes.OpClass>` in
:attr:`~ExclusionConstraint.expressions`::
ExclusionConstraint(
name='exclude_overlapping_opclasses',
expressions=[(OpClass('circle', 'circle_ops'), RangeOperators.OVERLAPS)],
)
.. versionchanged:: 4.1
Support for specifying operator classes with the ``OpClass()`` expression
was added.
Examples
--------

View file

@ -150,10 +150,10 @@ available from the ``django.contrib.postgres.indexes`` module.
.. class:: OpClass(expression, name)
An ``OpClass()`` expression represents the ``expression`` with a custom
`operator class`_ that can be used to define functional indexes or unique
constraints. To use it, you need to add ``'django.contrib.postgres'`` in
your :setting:`INSTALLED_APPS`. Set the ``name`` parameter to the name of
the `operator class`_.
`operator class`_ that can be used to define functional indexes, functional
unique constraints, or exclusion constraints. To use it, you need to add
``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`. Set the
``name`` parameter to the name of the `operator class`_.
For example::
@ -163,8 +163,7 @@ available from the ``django.contrib.postgres.indexes`` module.
)
creates an index on ``Lower('username')`` using ``varchar_pattern_ops``.
Another example::
::
UniqueConstraint(
OpClass(Upper('description'), name='text_pattern_ops'),
@ -173,9 +172,23 @@ available from the ``django.contrib.postgres.indexes`` module.
creates a unique constraint on ``Upper('description')`` using
``text_pattern_ops``.
::
ExclusionConstraint(
name='exclude_overlapping_ops',
expressions=[
(OpClass('circle', name='circle_ops'), RangeOperators.OVERLAPS),
],
)
creates an exclusion constraint on ``circle`` using ``circle_ops``.
.. versionchanged:: 4.0
Support for functional unique constraints was added.
.. versionchanged:: 4.1
Support for exclusion constraints was added.
.. _operator class: https://www.postgresql.org/docs/current/indexes-opclass.html