To a single record

Relationships that point to a single record are displayed in the GUI as links.

Once configured, they can be used in charts ↗, filters ↗, scopes ↗, and segments ↗.

Many-to-One relation in the table view

Note that, for performance reasons when sorting a table-view on customizer-defined relations, Forest Admin will always sort on the primary key of the related collection.

Many-to-One relations

Many-to-One relations are by far the most common type of relation: many records from a collection are connected to one record in another.

Think about countries and towns: a town belongs to a single country, but each country can have multiple towns.

agent.customizeCollection('towns', collection =>
  collection.addManyToOneRelation('myCountry', 'countries', {
    foreignKey: 'country_id',
    foreignKeyTarget: 'id', // Optional (uses `country` primary key by default)
  }),
);

One-to-One relations

In a one-to-one relation, there is a one-to-one mapping between records in two collections. The relation can be unset for some records, but no record from the first collection can be linked to more than one record in the other collection.

Think about persons and passports: A person can have at most one passport, and each passport belongs to a single person.

Take note that the inverse of a one-to-one is a many-to-one.

This may seem counter-intuitive: the side of the relation which should be configured as many-to-one is the one that carries the foreign key.

// Configure one side of the relation ...
agent.customizeCollection('persons', collection => {
  collection.addOneToOneRelation('myPassport', 'passports', {
    originKey: 'person_id',
    originKeyTarget: 'id', // Optional (uses `persons` primary key by default)
  });
});

// ... and the other one.
agent.customizeCollection('passports', collection => {
  // ⚠️ Not 'OneToOne'
  collection.addManyToOneRelation('myOwner', 'customer', {
    foreignKey: 'person_id',
    foreignKeyTarget: 'id', // Optional (uses `persons` primary key by default)
  });
});

Last updated

Revision created

docs(datasource-sql): document introspection process