Relationship field

The relationship field is a reference to the slug of a specific collection entry. It renders a combobox in the Admin UI.

To create a one-to-many relationship, wrap the relationship field inside an array field.

⚠️

The collection string must match one of the keys used in the collections config object.

☝️

Heads up: the relationship field will only store a static string representing the slug of the selected collection entry.

If the slug of the entry in question changes, the stored value will not be updated. In other words, the relationship will be broken.

Usage examples

Example of has-one relationship:

country: fields.relationship({
  label: 'Country',
  description: 'The country this person lives in',
  collection: 'countries'
})

Example of has-many relationship:

authors: fields.array(
  fields.relationship({
    label: 'Authors',
    description: 'A list of authors for this post',
    collection: 'posts'
  }), {
    label: 'Authors',
    itemLabel: props => props.value
  }
)

Screencast walk-through

This segment of the Keystatic Mini-Course on YouTube may help understand how the relationship field works:


Type signature

Find the latest version of this field's type signature at: https://docsmill.dev/npm/@keystatic/core@latest#/.fields.relationship