Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Constraints

Constraints restrict the values an attribute can hold. They are specified after the type in an attribute definition.

Syntax

<name>: <type> [NOT NULL [ERROR '<message>']] [UNIQUE [ERROR '<message>']] [DEFAULT <value>]

Constraints must appear in this order:

  1. NOT NULL (optionally with ERROR)
  2. UNIQUE (optionally with ERROR)
  3. DEFAULT

NOT NULL

Marks the attribute as required. The value cannot be empty.

Name: String(200) NOT NULL

With a custom error message displayed to the user:

Name: String(200) NOT NULL ERROR 'Name is required'

UNIQUE

Enforces that the value must be unique across all objects of the entity.

Email: String(200) UNIQUE

With a custom error message:

Email: String(200) UNIQUE ERROR 'Email already exists'

DEFAULT

Sets the initial value when a new object is created.

Count: Integer DEFAULT 0
IsActive: Boolean DEFAULT TRUE
Status: Enumeration(Sales.OrderStatus) DEFAULT 'Draft'
Name: String(200) DEFAULT 'Unknown'

Default value syntax varies by type:

TypeDefault SyntaxExamples
StringDEFAULT 'value'DEFAULT '', DEFAULT 'Unknown'
IntegerDEFAULT nDEFAULT 0, DEFAULT -1
LongDEFAULT nDEFAULT 0
DecimalDEFAULT n.nDEFAULT 0, DEFAULT 0.00, DEFAULT 99.99
BooleanDEFAULT TRUE/FALSEDEFAULT TRUE, DEFAULT FALSE
AutoNumberDEFAULT nDEFAULT 1 (starting value)
EnumerationDEFAULT Module.Enum.ValueDEFAULT Shop.Status.Active, DEFAULT 'Pending'

Omitting the DEFAULT clause means no default value is set:

OptionalField: String(200)

Combining Constraints

All three constraints can be used together:

Email: String(200) NOT NULL ERROR 'Email is required'
                   UNIQUE ERROR 'Email already exists'
                   DEFAULT ''

More examples:

CREATE PERSISTENT ENTITY Sales.Product (
  -- Required only
  Name: String(200) NOT NULL,

  -- Required with custom error
  SKU: String(50) NOT NULL ERROR 'SKU is required for all products',

  -- Unique only
  Barcode: String(50) UNIQUE,

  -- Required and unique with custom errors
  ProductCode: String(20) NOT NULL ERROR 'Product code required'
                          UNIQUE ERROR 'Product code must be unique',

  -- Default only
  Quantity: Integer DEFAULT 0,

  -- No constraints
  Description: String(unlimited)
);

Validation Rule Mapping

Under the hood, constraints map to Mendix validation rules:

MDL ConstraintMendix Validation Rule
NOT NULLDomainModels$RequiredRuleInfo
UNIQUEDomainModels$UniqueRuleInfo

See Also