Ask AI


This section describes the SDL declarations pertaining to constraints.

Declare an abstract constraint:

abstract constraint min_value(min: anytype) {
    errmessage :=
        'Minimum allowed value for {__subject__} is {min}.';

    using (__subject__ >= min);

Declare a concrete constraint on an integer type:

scalar type posint64 extending int64 {
    constraint min_value(0);

Declare a concrete constraint on an object type:

type Vector {
    required x: float64;
    required y: float64;
    constraint expression on (
        __subject__.x^2 + __subject__.y^2 < 25

Define a constraint corresponding to the more explicit DDL commands.

[{abstract | delegated}] constraint name [ ( [argspec] [, ...] ) ]
    [ on ( subject-expr ) ]
    [ except ( except-expr ) ]
    [ extending base [, ...] ]
    [ using constr-expression ; ]
    [ errmessage := error-message ; ]
    [ annotation-declarations ]
    [ ... ]
"}" ;

where argspec is:

[ argname: ] {argtype | argvalue}

This declaration defines a new constraint with the following options:


If specified, the constraint will be abstract.


If specified, the constraint is defined as delegated, which means that it will not be enforced on the type it’s declared on, and the enforcement will be delegated to the subtypes of this type. This is particularly useful for exclusive constraints in abstract types. This is only valid for concrete constraints.


The name (optionally module-qualified) of the new constraint.


An optional list of constraint arguments.

For an abstract constraint argname optionally specifies the argument name and argtype specifies the argument type.

For a concrete constraint argname optionally specifies the argument name and argvalue specifies the argument value. The argument value specification must match the parameter declaration of the abstract constraint.

on ( subject-expr )

An optional expression defining the subject of the constraint. If not specified, the subject is the value of the schema item on which the concrete constraint is defined. The expression must refer to the original subject of the constraint as __subject__. Note also that <subject-expr> itself has to be parenthesized.

except ( exception-expr )
An optional expression defining a condition to create exceptions

to the constraint. If <exception-expr> evaluates to true, the constraint is ignored for the current subject. If it evaluates to false or {}, the constraint applies normally.

except may only be declared on object constraints, and is otherwise follows the same rules as on, above.

extending base [, ...]

If specified, declares the parent constraints for this abstract constraint.

The valid SDL sub-declarations are listed below:

using constr_expression

A boolean expression that returns true for valid data and false for invalid data. The expression may refer to the subject of the constraint as __subject__. This declaration is only valid for abstract constraints.

errmessage := error_message

An optional string literal defining the error message template that is raised when the constraint is violated. The template is a formatted string that may refer to constraint context variables in curly braces. The template may refer to the following:

  • $argname – the value of the specified constraint argument

  • __subject__ – the value of the title annotation of the scalar type, property or link on which the constraint is defined.

If the content of curly braces does not match any variables, the curly braces are emitted as-is. They can also be escaped by using double curly braces.


Set constraint annotation to a given value.