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
primaryfield - Primary keys are automatically
uniqueand non-null - Primary keys cannot be modified after creation
PRIMARY KEYPrimary 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
NULLvalues are considered distinct (multiple NULLs allowed if nullable)- Primary keys are implicitly unique
UNIQUEUnique 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)
NULL (default is NOT NULL)Nullable Best Practices
Default Values
Specify default values for fields:constraint
Provides a default value when the field is not specified.Syntax:Available defaults:SQL Mapping:
now()- Current timestamp (fortimestampfields)- Literal values (strings, numbers, booleans)
DEFAULT valueCurrent 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 fieldPrimary Keys Non-Nullable - Primary keys cannot be
nullablePrimary Keys Are Unique - Primary keys are implicitly
uniqueType 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
Common Patterns
Soft Deletes
Audit Timestamps
Optional Relations
Natural Keys
Next Steps
Field Types
Learn about available field types
Annotations
Optimize with backend annotations