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

Attributes and Validation Rules

Attributes define the fields on an entity. Each attribute has a name, a type, and optional constraints.

Attribute Syntax

[/** <documentation> */]
<name>: <type> [NOT NULL [ERROR '<message>']] [UNIQUE [ERROR '<message>']] [DEFAULT <value>]
ComponentDescription
NameAttribute identifier (follows identifier rules)
TypeOne of the primitive types or an enumeration reference
NOT NULLValue is required
UNIQUEValue must be unique across all objects
DEFAULTInitial value on object creation
/** ... */Documentation comment

Attribute Ordering

Attributes are listed in order, separated by commas. The last attribute has no trailing comma:

CREATE PERSISTENT ENTITY Module.Entity (
  FirstAttr: String(200),      -- comma after
  SecondAttr: Integer,         -- comma after
  LastAttr: Boolean DEFAULT FALSE  -- no comma
);

Validation Rules

Validation rules are expressed as attribute constraints. When validation fails, Mendix displays the error message (if provided) or a default message.

ValidationMDL SyntaxDescription
RequiredNOT NULLAttribute must have a value
Required with messageNOT NULL ERROR 'message'Custom error message on empty
UniqueUNIQUEValue must be unique across all objects
Unique with messageUNIQUE ERROR 'message'Custom error message on duplicate

Example with Validation

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',

  -- Optional field (no validation)
  Description: String(unlimited)
);

Documentation Comments

Attach documentation to individual attributes with /** ... */:

CREATE PERSISTENT ENTITY Sales.Customer (
  /** Unique customer identifier, auto-generated */
  CustomerId: AutoNumber NOT NULL UNIQUE DEFAULT 1,

  /** Full legal name of the customer */
  Name: String(200) NOT NULL,

  /** Primary contact email address */
  Email: String(200) UNIQUE
);

CALCULATED Attributes

An attribute can be marked as calculated, meaning its value is computed by a microflow rather than stored:

FullName: String(400) CALCULATED

Calculated attributes use a CALCULATED BY clause to specify the source microflow. See the MDL quick reference for details.

See Also