Skip to content

Commit 56398c4

Browse files
p7novpatiencedaur
andauthored
Add constraints docs (#2943)
Resolves #2627 Co-authored-by: Dia Patience Daur <[email protected]>
1 parent ef349ed commit 56398c4

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

doc/book/box/data_model.rst

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Full information is in section
388388
| | | |
389389
+--------------------------------+-------------------------------------------+--------------------------------------+
390390
| ``'integer'`` | :ref:`integer <index-box_integer>`, | TREE or HASH |
391-
| (may also be called int’) | which may include unsigned values | |
391+
| (may also be called ``'int'``) | which may include unsigned values | |
392392
| | | |
393393
| | | |
394394
+--------------------------------+-------------------------------------------+--------------------------------------+
@@ -554,6 +554,117 @@ Charts explaining the precise differences from DUCET order are
554554
in the
555555
`Common Language Data Repository <https://unicode.org/cldr/charts/30/collation>`_.
556556

557+
558+
.. _index-constraints:
559+
560+
-----------
561+
Constraints
562+
-----------
563+
564+
For better control over stored data, Tarantool supports **constraints** – user-defined
565+
limitations on the values of certain fields or entire tuples. Together with data types,
566+
constraints allow limiting the ranges of available field values both syntactically and semantically.
567+
568+
For example, the field ``age`` typically has the ``number`` type, so it cannot store
569+
strings or boolean values. However, it can still have values that don't make sense,
570+
such as negative numbers. This is where constraints come to help.
571+
572+
.. _index-constraint_types:
573+
574+
****************
575+
Constraint types
576+
****************
577+
578+
There are two types of constraints in Tarantool:
579+
580+
* *Field constraints* check that the value being assigned to a field
581+
satisfies a given condition. For example, ``age`` must be non-negative.
582+
583+
* *Tuple constraints* check complex conditions that can involve all fields of
584+
a tuple. For example, a tuple contains a date in three fields:
585+
``year``, ``month``, and ``day``. You can validate ``day`` values based on
586+
the ``month`` value (and even ``year`` if you consider leap years).
587+
588+
Field constraints work faster, while tuple constraints allow implementing
589+
a wider range of limitations.
590+
591+
.. _index-constraint_functions:
592+
593+
********************
594+
Constraint functions
595+
********************
596+
597+
Constraints use stored Lua functions, which must return ``true`` when the constraint
598+
is satisfied. Other return values (including ``nil``) and exceptions make the
599+
check fail and prevent tuple insertion or modification.
600+
601+
To create a constraint function, use :ref:`func.create with function body <box_schema-func_create_with-body>`.
602+
603+
Constraint functions take two parameters:
604+
605+
* The field value and the constraint name for field constraints.
606+
607+
.. code-block:: tarantoolsession
608+
609+
tarantool> box.schema.func.create('check_age',
610+
> {language = 'LUA', is_deterministic = true, body = 'function(f, c) return (f >= 0 and f < 150) end'})
611+
---
612+
...
613+
614+
* The tuple and the constraint name for tuple constraints.
615+
616+
.. code-block:: tarantoolsession
617+
618+
tarantool> box.schema.func.create('check_person',
619+
> {language = 'LUA', is_deterministic = true, body = 'function(t, c) return (t.age >= 0 and #(t.name) > 3) end'})
620+
---
621+
...
622+
623+
.. warning::
624+
625+
Tarantool doesn't check field names used in tuple constraint functions.
626+
If a field referenced in a tuple constraint gets renamed, this constraint will break
627+
and prevent further insertions and modifications in the space.
628+
629+
.. _index-constraint_apply:
630+
631+
********************
632+
Creating constraints
633+
********************
634+
635+
To create a constraint in a space, specify the corresponding function's name
636+
in the ``constraint`` parameter:
637+
638+
* Field constraints: when setting up the space format:
639+
640+
.. code-block:: tarantoolsession
641+
642+
tarantool> box.space.person:format({
643+
> {name = 'id', type = 'number'},
644+
> {name = 'name', type = 'string'},
645+
> {name = 'age', type = 'number', constraint = 'check_age'},
646+
> })
647+
648+
* Tuple constraints: when creating or altering a space:
649+
650+
.. code-block:: tarantoolsession
651+
652+
tarantool> box.schema.space.create('person', { engine = 'memtx', constraint = 'check_tuple'})
653+
654+
In both cases, ``constraint`` can contain multiple function names passed as a tuple.
655+
Each constraint can have an optional name:
656+
657+
.. code-block:: lua
658+
659+
constraint = {'age_constraint' = 'check_age', 'name_constraint' = 'check_name'}
660+
661+
.. note::
662+
663+
When adding a constraint to an existing space with data, Tarantool checks it
664+
against the stored data. If there are fields or tuples that don't satisfy
665+
the constraint, it won't be applied to the space.
666+
667+
557668
.. _index-box_sequence:
558669

559670
--------------------------------------------------------------------------------

locale/ru/LC_MESSAGES/book/box/data_model.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ msgstr ":ref:`boolean <index-box_boolean>`"
676676
msgid ":ref:`TREE or HASH <box_index-type>`"
677677
msgstr ":ref:`TREE или HASH <box_index-type>`"
678678

679-
msgid "``'integer'`` (may also be called ‘int’)"
680-
msgstr "``'integer'`` (также может называться ‘int’)"
679+
msgid "``'integer'`` (may also be called ``‘int’``)"
680+
msgstr "``'integer'`` (также может называться ``‘int’``)"
681681

682682
msgid ":ref:`integer <index-box_integer>`, which may include unsigned values"
683683
msgstr ""

0 commit comments

Comments
 (0)