Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying polymorphic relationship with [all] not working in payload 3 #10678

Open
Twansparant opened this issue Jan 20, 2025 · 1 comment · May be fixed by #10704
Open

Querying polymorphic relationship with [all] not working in payload 3 #10678

Twansparant opened this issue Jan 20, 2025 · 1 comment · May be fixed by #10704
Assignees

Comments

@Twansparant
Copy link

Twansparant commented Jan 20, 2025

Describe the Bug

I'm currently in the process of migrating my Payload 2.28 project to Payload 3.14
In one of my field blocks, I have a Has Many - Polymorphic relationship field.

In Payload 2 I used to be able to run a query with multiple values (where the values can be either pages or products id's.) like this:

?where[polymorphic.value][all]=66e98b37e62a08c1d2caca54,66ed8d9e4e47342f77c96287

However running the exact same query in Payload 3 returns 0 docs.
My content has stayed the same (after running the mongo migration script)

Link to the code that reproduces this issue

https://github.com/Twansparant/payload

Reproduction Steps

  1. Run reproduction code
  2. Go the the example post in admin and select the example page & example product in the Polymorphic field.
  3. Publish the changes
  4. Lookup the ids for the example page & example product and replace them in the url of step 5
  5. Go to url: http://localhost:3000/api/posts?where[polymorphic.value][all]=[example-page-id],[example-product-id]
  6. The response has "totalDocs": 0 while it should have 1 containing the example post

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

Not sure

Environment Info

Payload: v3.17.1
Node.js: v22.6.0
Next.js: v15.1.5
@Twansparant Twansparant added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 20, 2025
@Twansparant Twansparant changed the title Querying polymorphic relationship with multiple values not wortking anymore in payload 3 Querying polymorphic relationship with [all] not working in payload 3 Jan 20, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jan 21, 2025
@Twansparant
Copy link
Author

Twansparant commented Jan 21, 2025

As addition, I also tried it with the qs-esm package like this:

import { stringify } from 'qs-esm'
import type { Where } from 'payload'

const query: Where = {
  'polymorphic.value': {
    all: '66e98b37e62a08c1d2caca54,66ed8d9e4e47342f77c96287',
  }
}

const getPosts = async () => {
  const stringifiedQuery = stringify(
    {
      where: query,
    },
    { addQueryPrefix: true },
  )

  const response = await fetch(`http://localhost:3000/api/posts${stringifiedQuery}`)
}

Resulting in the same response:

{
	"docs": [],
	"totalDocs": 0,
	"limit": 10,
	"totalPages": 1,
	"page": 1,
	"pagingCounter": 1,
	"hasPrevPage": false,
	"hasNextPage": false,
	"prevPage": null,
	"nextPage": null
}

At the moment, the only way to make it work, is by quering it with the AND and contains operators:

const query: Where = {
  and: [
    {
      'polymorphic.value': {
        contains: '66e98b37e62a08c1d2caca54'
      }
    },
    {
      'polymorphic.value': {
        contains: '66ed8d9e4e47342f77c96287'
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants