payloadcms / payloadcms/payload

Polymorphic Relationships and Virtual Fields not handled properly

Open
#13,089 3 comments 1 reaction 1 assignee View on GitHub

@r1tsuu is already working on this.

Since Aug 1, 2025.

Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

I set up a collection with a "Has One - Polymorphic" relationship as per https://payloadcms.com/docs/fields/relationship#has-one-polymorphic. Both collections that can be referred have an identical virtual field created as per https://payloadcms.com/posts/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges. Finally, items in the collection that holds the relationship also have a virtual field created as per https://payloadcms.com/docs/fields/relationship#linking-virtual-fields-with-relationships that refers to the virtual fields in the other collection. This combination results in numerous issues.

The first issue is that the titles of the "referred to" options do not show up properly in the dropdown box when creating a new item, presumably because the virtual field is used as the "admin title":

Image

I can still successfully create an item though but then it seems the virtual field which refers to the related items does not work:

Image

Lastly I see that when I try to make another item, I still have all the options available in the dropdown box even though unique: true is set on the relationship field (I would assume the item that already has a link should no longer be in this list). Luckily when I try to create a second item that links to the same item then I do get an error at least, but it is not an informative one.

Link to the code that reproduces this issue

https://github.com/Gerharddc/payload-relationship-bug

Reproduction Steps

Create some "Users":

Image

Create some "Visistors":

Image

Make a new "Person" by selecting one of the users or visitors (and observe that the titles are broken):

Image

Observe that the "Full Name" column is broken:

Image

Try to make a second "Person" and observe that all 4 options are still presented and that the titles are still broken. Then try to refer to the same user or visitor again and observe that an uninformative error appears:

Image
Which area(s) are affected? (Select all that apply)

area: ui

Environment Info
Binaries:
  Node: 22.16.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.46.0
  next: 15.3.2
  @payloadcms/email-nodemailer: 3.46.0
  @payloadcms/graphql: 3.46.0
  @payloadcms/next/utilities: 3.46.0
  @payloadcms/payload-cloud: 3.46.0
  @payloadcms/richtext-lexical: 3.46.0
  @payloadcms/translations: 3.46.0
  @payloadcms/ui/shared: 3.46.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32510
  Available CPU cores: 20

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.