Defining Relations in Collections

Relations in Payload CMS allow one collection to reference another, creating links between different sets of data. The most common types of relationships include:

  • One-to-One (A single reference to another document)
  • One-to-Many (A field can reference multiple documents)
  • Many-to-Many (Multiple documents can reference multiple other documents)

To define a relation, use the relationship field type in a collection schema.

Example:

import { CollectionConfig } from 'payload/types';

const Users: CollectionConfig = {
  slug: 'users',
  fields: [
    {
      name: 'profile',
      type: 'relationship',
      relationTo: 'profiles',
    },
  ],
};

export default Users;

In this example, the users collection has a profile field that relates to the profiles collection.

Leave a comment

Your email address will not be published. Required fields are marked *