Concepts · 0.8.1

Relationships between entities

How belongsTo, hasMany and manyToMany work, where links are stored and what each side can change.

On this page

A relationship connects ENTITY_NAME and RELATED_ENTITY_NAME records. The model uses a logical name RELATION_NAME, the database stores the reference in FIELD_NAME, and the interface shows a selected label field. Changing the label does not create a table; changing physical storage affects the schema.

Creating a relationship

In Content, select the owning entity and click Create relation:

  • Many-to-one: a reference to one record of another entity.
  • One-to-many view: an inverse list based on an existing reference.
  • Many-to-many: related records through a join table.

Choose the target entity, requirement and record label field. Save with Finish. See the related-record UI walkthrough.

Relationship storage

For belongsTo, the logical relationship ENTITY_NAME.RELATION_NAME uses the foreign-key field ENTITY_NAME.FIELD_NAME. The inverse collection RELATED_ENTITY_NAME.INVERSE_COLLECTION_NAME displays source records through that same key. It creates no second FK.

For manyToMany, COLLECTION_NAME is the logical collection name and JOIN_TABLE_NAME is its SQL join table; there is one owner. sourceField and targetField reference entity primary keys. Change membership on the owner; the inverse view reads the same relationship. The current contract cannot use an alternative unique key instead of the single PK.

Database checks

For belongsTo, enforce=true enables a physical foreign-key constraint. Available actions:

Event Options
Related-record deletion: on-delete restrict rejects deletion; setNull clears the reference.
Key change: on-update restrict rejects changes; cascade updates references.

setNull requires an optional reference with coordinated settings. displayField chooses a user-facing label and does not match keys.

The generator may block changes to an already-generated physical relationship. Inspect the plan and diagnostic: existing data may require a separate migration. Force does not replace that migration.

The inverse side

For manyToMany, inverseName defines the inverse model name. inverseDisplayField enables a readable inverse view, for example ENTITY_NAME.title in a RELATED_ENTITY_NAME card. It displays relationships without editing membership and creates no additional join table.

The admin supports bounded related-record search, permitted filters and Related records. Empty collections, unavailable records and load errors represent different situations. The server checks access to both entities.

Applying changes

Create entities and compatible keys, configure the owner, then add inverse views. Review the plan, Generate and apply migrations.

For a NOT VALID constraint, find and fix violating rows, then run explicit validation. Do not delete data to bypass validation. See migrations.

Interface settings are in Content: relationships; commands for the same model are in CLI: relationships.