Skip to content

Virtual relationship and upload fields with a reference are never populated #19251

Open

Description

@digitalwerber

Describe the Bug

A relationship or upload field with a virtual reference (e.g. { type: 'upload', relationTo: 'media', virtual: 'place.image' }) is never populated. It returns the raw ID at every depth, while the referenced field itself (place.image) is populated at depth >= 2. According to the docs on virtual fields, such a field should behave like a regular relationship or upload field and return the populated document at depth >= 1. This also means it cannot be used as admin.useAsThumbnail.

Link to the code that reproduces this issue

https://github.com/digitalwerber/payload/blob/fix/virtual-relationship-population/test/database/int.spec.ts (test "should populate a relationship virtual field with reference like a relationship field")

Reproduction Steps

  1. Use this config:
collections: [
  { slug: 'media', upload: true, fields: [] },
  {
    slug: 'places',
    fields: [
      { name: 'title', type: 'text' },
      { name: 'image', type: 'upload', relationTo: 'media' },
    ],
  },
  {
    slug: 'events',
    fields: [
      { name: 'place', type: 'relationship', relationTo: 'places' },
      { name: 'placeImage', type: 'upload', relationTo: 'media', virtual: 'place.image' },
      { name: 'placeTitle', type: 'text', virtual: 'place.title' },
    ],
  },
]
  1. Create a media document, a place with image set to it, and an event with place set to the place.
  2. Call payload.findByID({ collection: 'events', id, depth }) with depth 0 to 3.
  3. placeImage is the media ID at every depth. placeTitle resolves as expected.

Which area(s) are affected?

area: core

Environment Info

payload: 4.0.0-beta.6 (also current main)
node: 24.22.1

Not adapter specific: reproduced on SQLite and MongoDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugPayload OSSarea: core

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions