Concepts

Relationships

One-to-many, many-to-one and one-to-one links between perspectives, kept consistent by the database.

A relationship links two perspectives, and the database keeps it consistent on every add, update and delete.

Ends and kinds

A relationship has a name, a display name and two ends. Each end is a perspective (stored by its id) and a multiplicity:

EndNeeds
OneOnly the perspective.
ManyThe perspective and a field: a Reference field of that perspective, holding the id of the entity at the One end.
KindEndsExampleWhere the reference lives
One-to-manyOne → ManyCustomer → OrderOrder.customer holds the customer's id.
Many-to-oneMany → OneStaff → Staff (manager)Staff.manager holds the manager's id.
One-to-oneOne → OneBadge → StaffExactly one end names a field (Badge.holder). No two badges may refer to the same staff member.

Many-to-many relationships aren't supported yet.

What's enforced

All checks run before anything is written; a change that breaks a rule stores nothing.

OperationCheck
Add or update an entity with the referencing perspectiveIf the reference field is set, it must be the id of an existing entity with the referenced perspective. An entity may refer to itself if it has both. In a one-to-one relationship, no other entity may already refer to the same target.
Remove the referenced perspective from an entityRefused while others still refer to it.
Delete an entityRefused while others refer to it (restrict). Delete or re-point them first.

Violations are a 409 Conflict with an errors list.

Optional or mandatory

That's up to the perspective: mark the reference field required on the referencing perspective to make the link mandatory. Otherwise a missing (or null) reference is allowed.

Defining a relationship checks the data

Defining (or redefining) a relationship checks the entities already stored. If any break it, the definition is refused and the problems are listed (up to 20).

The reference field is indexed automatically, so "who refers to this entity?" is an index lookup. While the relationship exists, its perspective can't drop the reference field, and the index can't be dropped.

Relationships in the tools

  • Views show a relationship column as the name of the entity it refers to (its first three Text fields with a value), not its id. In the portal the name is a link to that entity.
  • Forms offer a sub-grid where the perspective is on the One side (a customer's orders) and a lookup where it holds the reference (an order's customer). See Forms.
  • A relationship between a perspective and itself, such as Staff → manager, is offered both ways.

Defining relationships

  • On the maker site: Relationships → New relationship….
  • With occtl: occtl schema relationship set CustomerOrders --from Order:Many:customer --to Customer:One.
  • With the API:
http
PUT /api/schema/relationships/CustomerOrders
Content-Type: application/json

{
  "displayName": "Orders",
  "from": { "interface": "Customer", "multiplicity": "One" },
  "to": { "interface": "Order", "multiplicity": "Many", "field": "customer" }
}

Defining or deleting a relationship needs Manage on both perspectives. Deleting one keeps the data and the field's index.

A refused delete confirms that some entity refers to the target, even one the caller can't see. No ids or counts are given.