Skip to main content
Constraints enforce data integrity rules at the schema level. ChameleonDB validates constraints both at compile time and runtime.

Primary Keys

Every entity must have exactly one primary key:
constraint
Designates a field as the entityโ€™s primary key.Syntax:
Example:
Rules:
  • Each entity must have exactly one primary field
  • Primary keys are automatically unique and non-null
  • Primary keys cannot be modified after creation
SQL Mapping: PRIMARY KEY

Primary Key Best Practices

Unique Constraints

Enforce uniqueness across all rows:
constraint
Ensures field values are unique across all entities.Syntax:
Example:
Rules:
  • Unique fields cannot have duplicate values
  • NULL values are considered distinct (multiple NULLs allowed if nullable)
  • Primary keys are implicitly unique
SQL Mapping: UNIQUE

Unique Constraint Examples

Nullable Fields

Allow fields to have NULL values:
constraint
Allows a field to be NULL (empty/missing).Syntax:
Example:
Rules:
  • Fields are NOT NULL by default
  • Primary keys cannot be nullable
  • Foreign keys can be nullable (optional relations)
SQL Mapping: NULL (default is NOT NULL)

Nullable Best Practices

Use nullable sparingly. Non-nullable fields make your domain model clearer and prevent many runtime errors.

Default Values

Specify default values for fields:
constraint
Provides a default value when the field is not specified.Syntax:
Available defaults:
  • now() - Current timestamp (for timestamp fields)
  • Literal values (strings, numbers, booleans)
Example:
SQL Mapping: DEFAULT value

Current Support (v1.0)

Currently, only default now() is fully supported for timestamp fields. Additional default value types are planned for future releases.

Combining Constraints

Constraints can be combined on a single field:

Constraint Order

Constraints can appear in any order:
The field type must always come immediately after the colon (:). Constraints follow the type.

Complete Example

Hereโ€™s a complete schema demonstrating all constraints:

Validation Rules

ChameleonDB enforces these validation rules:
Exactly One Primary Key - Each entity must have exactly one primary field
Primary Keys Non-Nullable - Primary keys cannot be nullable
Primary Keys Are Unique - Primary keys are implicitly unique
Type Compatibility - Default values must match the field type
Constraint Conflicts - Constraints must not conflict (e.g., primary nullable is invalid)

Runtime Enforcement

Constraints are enforced at runtime:

Best Practices

Use UUIDs for primary keys - UUIDs prevent conflicts and are globally unique
Mark optional fields as nullable - Be explicit about which fields can be empty
Use unique constraints for natural keys - Email addresses, usernames, SKUs, etc.
Use default now() for timestamps - Automatically track creation and update times
Avoid nullable on business-critical fields - Required fields make your domain model clearer and prevent bugs.

Common Patterns

Soft Deletes

Audit Timestamps

Optional Relations

Natural Keys

Next Steps

Field Types

Learn about available field types

Annotations

Optimize with backend annotations