payload icon indicating copy to clipboard operation
payload copied to clipboard

Is in filter acts as equals for relationship fields

Open stofolus opened this issue 1 year ago • 8 comments

Describe the Bug

When filtering a collection table on a relationship field using "is in" it behaves just like the "equals" option.

Image

Link to the code that reproduces this issue

https://github.com/stofolus/payload-filter-in

Reproduction Steps

Use the base template. Add the following fields to the media collection

{
  name: 'user',
  type: 'relationship',
  relationTo: 'users',
},
{
  name: 'language',
  type: 'select',
  options: ['en', 'fr', 'es', 'de', 'it', 'pt', 'ru', 'zh', 'ja', 'ko'],
},

Try to filter using "is in" on both fields. My expectation is that it should work in the same way

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

area: ui

Environment Info

Binaries:
  Node: 22.13.0
  npm: 11.0.0
  Yarn: N/A
  pnpm: 10.8.0
Relevant Packages:
  payload: 3.58.0
  next: 15.4.4
  @payloadcms/db-mongodb: 3.58.0
  @payloadcms/email-nodemailer: 3.58.0
  @payloadcms/graphql: 3.58.0
  @payloadcms/next/utilities: 3.58.0
  @payloadcms/payload-cloud: 3.58.0
  @payloadcms/richtext-lexical: 3.58.0
  @payloadcms/translations: 3.58.0
  @payloadcms/ui/shared: 3.58.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024
  Available memory (MB): 30891
  Available CPU cores: 16

stofolus avatar Oct 07 '25 13:10 stofolus

Can you share your expected outcome? The screen shot is a bit vague.

JarrodMFlesch avatar Oct 10 '25 12:10 JarrodMFlesch

My expected outcome is to be able to select multiple. As you can see in the screenshot if I use a select I can select many but if I use a relationship I can only choose one

I'm pretty sure that using is in on relationships used to have a multi select

stofolus avatar Oct 10 '25 12:10 stofolus

To select multiple values, the relationship field must be defined with hasMany: true. For single relationships (hasMany: false), the current behavior is expected: the table filter only presents a single-value selector, which accurately reflects that each document stores a single reference.

Closing this issue as expected behavior.

JessRynkar avatar Feb 10 '26 17:02 JessRynkar

You have misunderstood what this issue is about. It's when filtering in the list view using the in operator.

It is however fine to close it as this has been solved in a recent version of payload and it now works as expected. Allowing us to select multiple values when filtering using the in operator in the UI

stofolus avatar Feb 10 '26 18:02 stofolus

I'm still seeing this as an issue v3.76.1.

@JessRynkar As @stofolus has said, the issue is that when using an is in filter, relationship fields with hasMany: false doesn't allow you to select multiple options for the is in query.

Asheboy avatar Feb 12 '26 13:02 Asheboy

Ohh yeah you're right. When I tested it I did so on a hasMany: true field. This is definitely still a bug.

If I have a field like this:

{
  type: "relationship",
  name: "role",
  label: "Role",
  relationTo: "roles",
},

I can query it like this

payload.find({
  collection: "admins",
  where: {
    roles: {
      in: ["Sales", "SuperAdmin"],
    },
  },
})

There is no reason that this should work in the UI only when hasMany is true.

stofolus avatar Feb 12 '26 14:02 stofolus

I'm patching it with something like this, if it's any help to you:

index 18dfc7a..d969aec 100644
--- a/node_modules/@payloadcms/ui/dist/elements/WhereBuilder/Condition/Relationship/index.js
+++ b/node_modules/@payloadcms/ui/dist/elements/WhereBuilder/Condition/Relationship/index.js
@@ -24,6 +24,7 @@ export const RelationshipFilter = props => {
     },
     filterOptions,
     onChange,
+    operator,
     value
   } = props;
   const placeholder = 'placeholder' in admin ? admin?.placeholder : undefined;
@@ -47,6 +48,7 @@ export const RelationshipFilter = props => {
     t
   } = useTranslation();
   const locale = useLocale();
+  const isMulti = hasMany || ['in', 'not_in'].includes(operator);
   const relationSlugs = hasMultipleRelations ? relationTo : [relationTo];
   const loadedRelationships = React.useRef(new Map(relationSlugs.map(relation => [relation, {
     hasLoadedAll: false,
@@ -150,7 +152,7 @@ export const RelationshipFilter = props => {
   }, []);
   const findOptionsByValue = useCallback(() => {
     if (value) {
-      if (hasMany) {
+      if (isMulti) {
         if (Array.isArray(value)) {
           return value.map(val => {
             if (hasMultipleRelations) {
@@ -192,7 +194,7 @@ export const RelationshipFilter = props => {
       return options.find(opt_2 => opt_2.value == value);
     }
     return undefined;
-  }, [hasMany, hasMultipleRelations, value, options]);
+  }, [isMulti, hasMultipleRelations, value, options]);
   const handleInputChange = useCallback(input => {
     if (input !== search) {
       dispatchOptions({
@@ -276,7 +278,7 @@ export const RelationshipFilter = props => {
   */
   useEffect(() => {
     if (value && hasLoadedFirstOptions) {
-      if (hasMany) {
+      if (isMulti) {
         const matchedOptions = findOptionsByValue();
         (matchedOptions || []).forEach((option, i) => {
           if (!option) {
@@ -299,7 +301,12 @@ export const RelationshipFilter = props => {
         }
       }
     }
-  }, [addOptionByID, findOptionsByValue, hasMany, hasMultipleRelations, relationTo, value, hasLoadedFirstOptions]);
+  }, [addOptionByID, findOptionsByValue, isMulti, hasMultipleRelations, relationTo, value, hasLoadedFirstOptions]);
+  useEffect(() => {
+    if (!isMulti && Array.isArray(value)) {
+      onChange(value[0] ?? null);
+    }
+  }, [isMulti, onChange, value]);
   const classes = ['field-type', baseClass, errorLoading && 'error-loading'].filter(Boolean).join(' ');
   const valueToRender = findOptionsByValue() || value;
   return /*#__PURE__*/_jsx("div", {
@@ -309,14 +316,14 @@ export const RelationshipFilter = props => {
       children: errorLoading
     }) : /*#__PURE__*/_jsx(ReactSelect, {
       disabled: disabled,
-      isMulti: hasMany,
+      isMulti: isMulti,
       isSortable: isSortable,
       onChange: selected => {
         if (!selected) {
           onChange(null);
           return;
         }
-        if (hasMany && Array.isArray(selected)) {
+        if (isMulti && Array.isArray(selected)) {
           onChange(selected ? selected.map(option_0 => {
             if (hasMultipleRelations) {
               return {

Asheboy avatar Feb 12 '26 14:02 Asheboy

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

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