via keyword.
Relation Types
ChameleonDB supports two primary relation patterns:One-to-Many (Has Many)
Defines a collection relationship where one entity has multiple related entities:Many-to-One (Belongs To)
Defines a single relationship where one entity belongs to another:The via Keyword
The via keyword specifies the foreign key field in the related entity:
The
via keyword is only used for one-to-many relations (arrays). Many-to-one relations infer the foreign key automatically.Bidirectional Relations
Most relationships are bidirectional - defined from both sides:Foreign Key Fields
Foreign key fields must be explicitly declared:Nested Relations
Relations can be traversed through multiple levels:Self-Referential Relations
Entities can reference themselves:Complete Example
Here’s a complete e-commerce schema demonstrating all relation patterns:Relation Validation
ChameleonDB validates relations at compile time:Target Exists - The target entity must be defined in the schema
Foreign Key Type Match - Foreign key type must match the target’s primary key type
Foreign Key Exists - For
via relations, the foreign key field must exist in the target entityNo Circular Ownership - Entities cannot form circular ownership dependencies
Querying Relations
Relations are traversed using the query API:Eager Loading
Load related entities to avoid N+1 queries:Nested Includes
Multiple Relations
Best Practices
Common Patterns
Parent-Child
Author-Content
Order-Line Items
Limitations
Next Steps
Field Types
Learn about field types for foreign keys
Constraints
Add validation to relations