Search
ctrl/
Ask AI
Light
Dark
System

Constraints

This section describes the DDL commands pertaining to constraints.

Define a new abstract constraint.

[ with [ module-alias := ] module module-name ]
create abstract constraint name [ ( [argspec] [, ...] ) ]
  [ on ( subject-expr ) ]
  [ extending base [, ...] ]
"{" subcommand; [...] "}" ;

where argspec is:

  [ argname: ] argtype

where subcommand is one of

  using constr-expression
  set errmessage := error-message
  create annotation annotation-name := value

The command create abstract constraint defines a new abstract constraint.

If name is qualified with a module name, then the constraint is created in that module, otherwise it is created in the current module. The constraint name must be distinct from that of any existing schema item in the module.

Most sub-commands and options of this command are identical to the SDL constraint declaration, with some additional features listed below:

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

set errmessage := error_message

An optional string literal defining the error message template that is raised when the constraint is violated. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

create annotation annotation-name := value;

Set constraint annotation-name to value.

See create annotation for details.

Create an abstract constraint “uppercase” which checks if the subject is a string in upper case.

Copy
create abstract constraint uppercase {
    create annotation title := "Upper case constraint";
    using (str_upper(__subject__) = __subject__);
    set errmessage := "{__subject__} is not in upper case";
};

Alter the definition of an abstract constraint.

[ with [ module-alias := ] module module-name ]
alter abstract constraint name
"{" subcommand; [...] "}" ;

where subcommand is one of

  rename to newname
  using constr-expression
  set errmessage := error-message
  reset errmessage
  create annotation annotation-name := value
  alter annotation annotation-name := value
  drop annotation annotation-name

The command alter abstract constraint changes the definition of an abstract constraint item. name must be a name of an existing abstract constraint, optionally qualified with a module name.

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

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

The following subcommands are allowed in the alter abstract constraint block:

rename to newname

Change the name of the constraint to newname. All concrete constraints inheriting from this constraint are also renamed.

alter annotation annotation-name;

Alter constraint annotation-name. See alter annotation for details.

drop annotation annotation-name;

Remove constraint annotation-name. See drop annotation for details.

reset errmessage;

Remove the error message from this abstract constraint. The error message specified in the base abstract constraint will be used instead.

All the subcommands allowed in a create abstract constraint block are also valid subcommands for an alter abstract constraint block.

Rename the abstract constraint “uppercase” to “upper_case”:

Copy
alter abstract constraint uppercase rename to upper_case;

Remove an abstract constraint from the schema.

[ with [ module-alias := ] module module-name ]
drop abstract constraint name ;

The command drop abstract constraint removes an existing abstract constraint item from the database schema. If any schema items depending on this constraint exist, the operation is refused.

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

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

Drop abstract constraint upper_case:

Copy
drop abstract constraint upper_case;

Define a concrete constraint on the specified schema item.

[ with [ module-alias := ] module module-name ]
create [ delegated ] constraint name
  [ ( [argspec] [, ...] ) ]
  [ on ( subject-expr ) ]
  [ except ( except-expr ) ]
"{" subcommand; [...] "}" ;

where argspec is:

  [ argname: ] argvalue

where subcommand is one of

  set errmessage := error-message
  create annotation annotation-name := value

The command create constraint defines a new concrete constraint. It can only be used in the context of create scalar type, alter scalar type, create property, alter property, create link, or alter link.

name must be a name (optionally module-qualified) of previously defined abstract constraint.

Most sub-commands and options of this command are identical to the SDL constraint declaration, with some additional features listed below:

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

set errmessage := error_message

An optional string literal defining the error message template that is raised when the constraint is violated. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

create annotation annotation-name := value;

An optional list of annotations for the constraint. See create annotation for details.

Create a “score” property on the “User” type with a minimum value constraint:

Copy
alter type User create property score -> int64 {
    create constraint min_value(0)
};

Create a Vector with a maximum magnitude:

Copy
create type Vector {
    create required property x -> float64;
    create required property y -> float64;
    create constraint expression ON (
        __subject__.x^2 + __subject__.y^2 < 25
    );
}

Alter the definition of a concrete constraint on the specified schema item.

[ with [ module-alias := ] module module-name [, ...] ]
alter constraint name
  [ ( [argspec] [, ...] ) ]
  [ on ( subject-expr ) ]
  [ except ( except-expr ) ]
"{" subcommand; [ ... ] "}" ;

-- or --

[ with [ module-alias := ] module module-name [, ...] ]
alter constraint name
  [ ( [argspec] [, ...] ) ]
  [ on ( subject-expr ) ]
  subcommand ;

where subcommand is one of:

  set delegated
  set not delegated
  set errmessage := error-message
  reset errmessage
  create annotation annotation-name := value
  alter annotation annotation-name
  drop annotation annotation-name

The command alter constraint changes the definition of a concrete constraint. As for most alter commands, both single- and multi-command forms are supported.

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the concrete constraint that is being altered.

argspec

A list of constraint arguments as specified at the time of create constraint.

on ( subject-expr )

A expression defining the subject of the constraint as specified at the time of create constraint.

The following subcommands are allowed in the alter constraint block:

set delegated

If set, 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.

set not delegated

If set, the constraint is defined as not delegated, which means that it will be enforced globally across the type it’s declared on and any extending types.

rename to newname

Change the name of the constraint to newname.

alter annotation annotation-name;

Alter constraint annotation-name. See alter annotation for details.

drop annotation annotation-name;

Remove an annotation. See drop annotation for details.

reset errmessage;

Remove the error message from this constraint. The error message specified in the abstract constraint will be used instead.

All the subcommands allowed in the create constraint block are also valid subcommands for alter constraint block.

Change the error message on the minimum value constraint on the property “score” of the “User” type:

Copy
alter type User alter property score
    alter constraint min_value(0)
        set errmessage := 'Score cannot be negative';

Remove a concrete constraint from the specified schema item.

[ with [ module-alias := ] module module-name [, ...] ]
drop constraint name
  [ ( [argspec] [, ...] ) ]
  [ on ( subject-expr ) ]
  [ except ( except-expr ) ] ;

The command drop constraint removes the specified constraint from its containing schema item.

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the migration definition. When module-alias is not specified, module-name becomes the effective current module and is used to resolve all unqualified names.

name

The name (optionally module-qualified) of the concrete constraint to remove.

argspec

A list of constraint arguments as specified at the time of create constraint.

on ( subject-expr )

A expression defining the subject of the constraint as specified at the time of create constraint.

Remove constraint “min_value” from the property “score” of the “User” type:

Copy
alter type User alter property score
    drop constraint min_value(0);