payload icon indicating copy to clipboard operation
payload copied to clipboard

Relationship fields are not searchable in the List View

Open mfilteau opened this issue 1 year ago • 5 comments

Describe the Bug

If we have a collection where a relationship field is identified as searchable:

export const Orders: CollectionConfig = {
  slug: 'orders',
  admin: {
    useAsTitle: 'orderNumber',
    listSearchableFields: ['customer'], // <--- field is relationship type
  },
  fields: [
    {
      name: 'orderNumber',
      type: 'text',
      required: true,
    },
    {
      name: 'date',
      type: 'date',
    },
    {
      name: 'product',
      type: 'relationship',
      relationTo: 'products',
    },
    {
      name: 'customer',
      type: 'relationship',
      relationTo: 'customers',
    },
  ],
}

The field appears to the user as searchable (in the list search placeholder) but nothing happens.

https://github.com/user-attachments/assets/e1656ab3-d081-44ea-a898-88982ebfaf6a

Link to the code that reproduces this issue

https://github.com/mfilteau/payload-bugs

Reproduction Steps

  1. Seed the database

http://localhost:3000/api/seed

  1. Go to the Orders list view in admin panel

  2. See that the list should be searchable by Customer

  3. Try entering a customer name in the search box.

Nothing happens.

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

area: ui

Environment Info

Binaries:
  Node: 20.10.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.17.1
  next: 15.1.0
  @payloadcms/db-mongodb: 3.17.1
  @payloadcms/email-nodemailer: 3.17.1
  @payloadcms/graphql: 3.17.1
  @payloadcms/next/utilities: 3.17.1
  @payloadcms/payload-cloud: 3.17.1
  @payloadcms/richtext-lexical: 3.17.1
  @payloadcms/translations: 3.17.1
  @payloadcms/ui/shared: 3.17.1
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 65457
  Available CPU cores: 16

mfilteau avatar Jan 17 '25 20:01 mfilteau

This isn't documented properly, but what you can do is include in your listSearchableFields the relationship properties you want to search. For example, instead of ['customer'] use ['customer.email', 'customer.name'] // etc.

This way you can search any fields of a relationship and get results back.

What we intend to do is add the searchable fields of the relationship automatically. That would either work by id, the useAsTitle field or the listSearchableFields of the related collection. This gets a little more complicated with polymorphic relationships, but that is something we can worry about later.

DanRibbens avatar Jan 21 '25 02:01 DanRibbens

Hi Dan,

I did change the listSearchableFields to 'customer.name' before but I got misled by the fact that the UI was showing a wrong placeholder:

Image

I just tried it again and the search works. It's just a placeholder bug now.

mfilteau avatar Jan 21 '25 17:01 mfilteau

I was able to get the related field to appear in the placeholder by updating this line: https://github.com/payloadcms/payload/blob/8113d3bdef16993c27bc572122dbffd9d7571897/packages/ui/src/elements/ListControls/getTextFieldsToBeSearched.ts#L18

to be:

const searchableFieldNames = new Set(
      listSearchableFields.map((field) => field.split('.').shift()),
    )

its not a proper solution, as it does nothing to validate the value in listSearchableFields is correct, nor does it identify the field that is being used for search (i.e, it appears in the search bar as "Customer" instead of "Email of Customer") . But it'd be great to see improvement added so this undocumented feature is at least a bit more usable

I have a branch prepared with this change and can submit a PR if you think it's acceptable @DanRibbens

dave-wwg avatar Sep 12 '25 21:09 dave-wwg

Hi @dave-wwg and @mfilteau, this thread hasn't had any activity in a while and it appears that the initial issue was resolved by Dan's comment regarding listSearchableFields.

Sounds like you both experienced a follow-up issue with the search placeholder; if either of you are still experiencing this on the latest version (v.3.74.0) please open a new issue with updated info/repro. Closing for now.

JessRynkar avatar Feb 02 '26 15:02 JessRynkar

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Feb 10 '26 05:02 github-actions[bot]