diff options
| author | Ian Foote <python@ian.feete.org> | 2016-11-05 13:12:12 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-07-10 15:32:33 -0400 |
| commit | 952f05a6db2665d83c04075119285f2164b03432 (patch) | |
| tree | 616ddfb21cd44c19292c025c494d1995afca13b8 /docs/ref | |
| parent | 6fbfb5cb9602574adc867d34241172226291d367 (diff) | |
Fixed #11964 -- Added support for database check constraints.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/checks.txt | 1 | ||||
| -rw-r--r-- | docs/ref/migration-operations.txt | 19 | ||||
| -rw-r--r-- | docs/ref/models/check-constraints.txt | 46 | ||||
| -rw-r--r-- | docs/ref/models/index.txt | 1 | ||||
| -rw-r--r-- | docs/ref/models/options.txt | 20 |
5 files changed, 87 insertions, 0 deletions
diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index b0d7dfc066..ee16ebde54 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -297,6 +297,7 @@ Models field accessor. * **models.E026**: The model cannot have more than one field with ``primary_key=True``. +* **models.W027**: ``<database>`` does not support check constraints. Security -------- diff --git a/docs/ref/migration-operations.txt b/docs/ref/migration-operations.txt index b45134b46d..c117145fde 100644 --- a/docs/ref/migration-operations.txt +++ b/docs/ref/migration-operations.txt @@ -207,6 +207,25 @@ Creates an index in the database table for the model with ``model_name``. Removes the index named ``name`` from the model with ``model_name``. +``AddConstraint`` +----------------- + +.. class:: AddConstraint(model_name, constraint) + +.. versionadded:: 2.2 + +Creates a constraint in the database table for the model with ``model_name``. +``constraint`` is an instance of :class:`~django.db.models.CheckConstraint`. + +``RemoveConstraint`` +-------------------- + +.. class:: RemoveConstraint(model_name, name) + +.. versionadded:: 2.2 + +Removes the constraint named ``name`` from the model with ``model_name``. + Special Operations ================== diff --git a/docs/ref/models/check-constraints.txt b/docs/ref/models/check-constraints.txt new file mode 100644 index 0000000000..29681d7ebc --- /dev/null +++ b/docs/ref/models/check-constraints.txt @@ -0,0 +1,46 @@ +=========================== +Check constraints reference +=========================== + +.. module:: django.db.models.constraints + +.. currentmodule:: django.db.models + +.. versionadded:: 2.2 + +The ``CheckConstraint`` class creates database check constraints. They are +added in the model :attr:`Meta.constraints +<django.db.models.Options.constraints>` option. This document +explains the API references of :class:`CheckConstraint`. + +.. admonition:: Referencing built-in constraints + + Constraints are defined in ``django.db.models.constraints``, but for + convenience they're imported into :mod:`django.db.models`. The standard + convention is to use ``from django.db import models`` and refer to the + constraints as ``models.CheckConstraint``. + +``CheckConstraint`` options +=========================== + +.. class:: CheckConstraint(constraint, name) + + Creates a check constraint in the database. + +``constraint`` +-------------- + +.. attribute:: CheckConstraint.constraint + +A :class:`Q` object that specifies the condition you want the constraint to +enforce. + +For example ``CheckConstraint(Q(age__gte=18), 'age_gte_18')`` ensures the age +field is never less than 18. + +``name`` +-------- + +.. attribute:: CheckConstraint.name + +The name of the constraint. diff --git a/docs/ref/models/index.txt b/docs/ref/models/index.txt index d731ee37dc..c3aa5a718a 100644 --- a/docs/ref/models/index.txt +++ b/docs/ref/models/index.txt @@ -9,6 +9,7 @@ Model API reference. For introductory material, see :doc:`/topics/db/models`. fields indexes + check-constraints meta relations class diff --git a/docs/ref/models/options.txt b/docs/ref/models/options.txt index cea73eb67f..246f3e7d9a 100644 --- a/docs/ref/models/options.txt +++ b/docs/ref/models/options.txt @@ -451,6 +451,26 @@ Django quotes column and table names behind the scenes. index_together = ["pub_date", "deadline"] +``constraints`` +--------------- + +.. attribute:: Options.constraints + + .. versionadded:: 2.2 + + A list of :doc:`constraints </ref/models/check-constraints>` that you want + to define on the model:: + + from django.db import models + + class Customer(models.Model): + age = models.IntegerField() + + class Meta: + constraints = [ + models.CheckConstraint(models.Q(age__gte=18), 'age_gte_18'), + ] + ``verbose_name`` ---------------- |
