@@ -388,7 +388,7 @@ Full information is in section
388
388
| | | |
389
389
+--------------------------------+-------------------------------------------+--------------------------------------+
390
390
| ``'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 | |
392
392
| | | |
393
393
| | | |
394
394
+--------------------------------+-------------------------------------------+--------------------------------------+
@@ -554,6 +554,117 @@ Charts explaining the precise differences from DUCET order are
554
554
in the
555
555
`Common Language Data Repository <https://unicode.org/cldr/charts/30/collation >`_.
556
556
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
+
557
668
.. _index-box_sequence :
558
669
559
670
--------------------------------------------------------------------------------
0 commit comments