Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Is it possible to query a 3 nested collection?

default discord avatar
johnmadrigal_2 years ago
7

I have these collections,



Location Group


Location


Post



Location Group has a value "title" string.


Location has a value "group" which relates to Location Group and "title" string.


Post has a value "location" which relates to Location.



When I'm trying to use dot notation on REST query on Post using location.locationGroup.title, it's not working, but when I use location.title, its working.



PLEASE HELP

  • discord user avatar
    jacobsfletch
    2 years ago
    @950202910095081473

    can you paste in your configs? You may I can help you write a query for this, which would look something like this

    ?where[location.group.title][equals]=hello&depth=3

    (depth might be your missing piece)

  • default discord avatar
    johnmadrigal_2 years ago

    Hello Jacob, thanks for responding. I am using your sample work name "website-cms" and I also found the same issue with your code.



    This is the repo code:


    https://github.com/payloadcms/website-cms/blob/main/src/collections/Posts.ts

    Can you create a Post record and create a query for "author.photo.alt"? Because in my end, even if I puth depth in the query, it still shows nothing.



    http://localhost:9000/api/posts?where[author.photo.alt][equals]=test&depth=3

    I'm suspecting that it is not allowed to query the 2nd nested collection, which in your code, the photo inside author which is under post.



    That is the exact issue i'm facing in my own payloadcms app.

  • discord user avatar
    jacobsfletch
    2 years ago

    Thanks for the description! Very helpful. That query

    should

    look like this:

    https://cms.payloadcms.com/api/posts?where[author.photo.alt][equals]=Write%20for%20the%20Community%20Blog%20Image

    but it appears to be broken at the moment 😢



    Go ahead and pull an issue in our repo if you'd like. We'll get this patched up.

  • default discord avatar
    johnmadrigal_2 years ago

    Okay thanks, but is this working before? or we really don't have feature like this?

  • discord user avatar
    jacobsfletch
    2 years ago

    This was working before so once we fix the issue we'll also write tests to prevent future regression.



    Update: this was fixed with

    https://github.com/payloadcms/payload/pull/2199
  • default discord avatar
    gigasproule5 months ago

    I don't mean to drag up an old issue, but this isn't working for me on v3. I have a collection of

    link

    , which has a one-to-many relationship with page collections (

    simple-page

    ,

    home-page

    , etc.) via a

    linkedPage

    field. If I try to query by

    linkedPage.id

    , it throws an error with the message

    The following path cannot be queried: linkedPage.id

    , which makes sense as the API has the

    linkedPage

    return an object of

    {relationTo: string; value: object}

    . If I try to query on

    linkedPage.relationTo

    , it works. However, if I try to query on

    linkedPage.value.id

    , I get the error again but with

    The following path cannot be queried: linkedPage.value.id

    . I've also tried

    linkedPage.value._status

    ,

    linkedPage._status

    and

    linkedPage.value.title

    (which is a field on the simple page), but none of them work.



    Edit: It looks like the nested relationship query works if there is only 1 type relationship, i.e. if

    linkedPage

    only

    allowed

    simple-page

    , it would be fine. But as it can have other types related to it, the query doesn't work.



    Edit 2: Looks like the

    value

    is the ID, which is why it can't query nested values, even with the

    depth

    specified appropriately.

  • default discord avatar
    nitrammets3 months ago

    Was this ever resolved?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.