DEV Community

Kacper Zawojski
Kacper Zawojski

Posted on

Payload virtual path fields: relationship data with zero extra columns

Any field in Payload can be marked virtual. The best version is virtual as a path — it automatically resolves the value from a relationship, with nothing written to the database:

{
  name: 'authorName',
  type: 'text',
  virtual: 'author.name', // walks through the `author` relationship down to `name`
}
Enter fullscreen mode Exit fullscreen mode

And when the path crosses a hasMany relationship, you get an array back:

{
  name: 'categoryTitles',
  type: 'text',
  hasMany: true,
  virtual: 'categories.title', // => ['Tech', 'News', 'Updates']
}
Enter fullscreen mode Exit fullscreen mode

The field shows up in your API responses and in your generated types — but it never takes up a column. Perfect for flattening relationship data without denormalizing your schema.

Top comments (0)