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.

Querying Multi-Collection Relationship Field Error

default discord avatar
notchrlast year
63

Hmm



Looking into it

  • default discord avatar
    mykz_last year

    Really appreciate it!

  • default discord avatar
    notchrlast year

    Though, if you got the

    Not supported

    error, it may be



    Seems like it should be though

  • default discord avatar
    mykz_last year

    Yeah, I thought maybe I have the query syntax wrong for relationship fields, but then it wouldn't work for a single relationTo.



    tried also

    'field.ID': {contains: 1}

    and:


    field: {
     collection1: {
      contains: 1,
     }
    },
  • default discord avatar
    notchrlast year

    This is postgres?

  • default discord avatar
    mykz_last year

    yeah, postgres

  • default discord avatar
    notchrlast year

    I'm searching for your exact error message, so far I've founmd



                throw new InvalidConfiguration(
                  'Unique is not supported in Postgres for hasMany text fields.',
                )
                throw new InvalidConfiguration(
                  'Unique is not supported in Postgres for hasMany number fields.',
                )


    what is your exact message



    ill try to match it with the codebase

  • default discord avatar
    mykz_last year
    Error: Not supported
  • default discord avatar
    notchrlast year

    interesting



    That's the payload error?

  • default discord avatar
    mykz_last year

    gives me 500 error

    /admin/collections/clubs?limit=10
  • default discord avatar
    mykz_last year

    I'm using the query in a access function return.



    Not sure if that matters, but wanted to mention it.

  • default discord avatar
    notchrlast year

    That's the only instance of that exact error message in the codebase from what I can see



    Whats the access function look like

  • default discord avatar
    mykz_last year
    import type { Access } from 'payload'
    
    export const accessTo: Access = ({ req }) => {
      return {
        created_for: {
          contains: 1,
        },
      }
    }


    {
      admin: {
        allowCreate: false,
        // @TODO Enable this when not debugging
        hidden: false,
      },
      hasMany: true,
      label: 'Created For',
      name: 'created_for',
      relationTo: ['partners', 'clubs'],
      required: true,
      type: 'relationship',
      index: true,
    },


    This is the field



    so I tested

    relationTo: 'partners'

    and it worked.

  • default discord avatar
    notchrlast year

    hmm

  • default discord avatar
    mykz_last year
  • default discord avatar
    notchrlast year

    I wonder if there is a workaround to get the data you need



    Yeah that seems to be the error



    Or where it gets thrown

  • default discord avatar
    mykz_last year

    yeah

  • default discord avatar
    notchrlast year

    We don't know which condition failed though

  • default discord avatar
    mykz_last year

    I have content that is going to created from different relations


    - Partners


    - Clubs


    - Groups



    So you could have a Partner create and Event or have a Club under a Partner create and Event etc.



    My plan was to have a switcher set a cookie or a preference on the user and the access controls would filter based on the partner, club, group in the created_for field.



    if that makes sense.



    So you can switch between a partner, club, or group and the access would filter the content based on reference fields value.



    it's fairly complex, but thought the query api would support a multi relationTo query.

  • default discord avatar
    notchrlast year

    It does seem odd that's not available

  • default discord avatar
    mykz_last year

    yeah

  • default discord avatar
    notchrlast year

    Though, you could tackle some of this logic. For instance, setting the user preference

  • default discord avatar
    mykz_last year

    why it makes me think my query syntax is wrong.

  • default discord avatar
    notchrlast year

    hmm



    maybe it's just contains?



    Would like..



    equals work?

  • default discord avatar
    mykz_last year

    no, same error for equals

  • default discord avatar
    notchrlast year

    and how about

    in

    in: [1, 2, 3]

  • default discord avatar
    mykz_last year

    same error



    I'm just looking through the code where the throw happens to see if I can meet a condition maybe.



    Doesn't look like it's supported tho.

  • default discord avatar
    notchrlast year

    hmm



    Based on those conditions?



    This is on v2 right?

  • default discord avatar
    mykz_last year

    no sorry v3



    also seems that the throw is for 'upload' case in the drizzle package on beta branch.



    sorry my bad it's for both



  • default discord avatar
    notchrlast year

    ok i created a v3 instance



    @434476210630950912

    So i created an Items collection



    a Fruits collection



    and a Vegetables collection



    How should i setup to mirror what youre attempting

  • default discord avatar
    mykz_last year

    Create a created_for field on your items collection that has this



    {
      admin: {
        allowCreate: false,
        // @TODO Enable this when not debugging
        hidden: false,
      },
      hasMany: true,
      label: 'Created For',
      name: 'created_for',
      relationTo: ['fruits', 'vegetables'],
      required: true,
      type: 'relationship',
      index: true,
    },
  • default discord avatar
    notchrlast year

    ok i did

  • default discord avatar
    mykz_last year

    then you can create a access function for the items collection



    import type { Access } from 'payload'
    
    export const accessTo: Access = ({ req }) => {
      return {
        created_for: {
          contains: 1,
        },
      }
    }


    create an item that at has a reference and then assign the access function and you should get the error when trying to view the list or single item.

  • default discord avatar
    notchrlast year


    like this



    then enable access?

  • default discord avatar
    mykz_last year

    yeah

  • default discord avatar
    notchrlast year

    for read?

  • default discord avatar
    mykz_last year

    yeah, or all I don't think it maters.

  • default discord avatar
    notchrlast year

    Error: formattedValue.replace is not a function

  • default discord avatar
    mykz_last year

    odd

  • default discord avatar
    notchrlast year
    import type { CollectionConfig } from 'payload'
    import { accessTo } from '../TestAccess'
    
    export const Items: CollectionConfig = {
      slug: 'items',
      admin: {
        useAsTitle: 'title',
      },
      access: {
        read: accessTo,
      },
      fields: [
        { name: 'title', type: 'text' },
        {
          admin: {
            allowCreate: false,
            // @TODO Enable this when not debugging
            hidden: false,
          },
          hasMany: true,
          label: 'Created For',
          name: 'created_for',
          relationTo: ['fruits', 'vegetables'],
          required: true,
          type: 'relationship',
        },
      ],
    }
  • default discord avatar
    mykz_last year

    whats your accessTo look like?

  • default discord avatar
    notchrlast year
    import type { Access } from 'payload'
    
    export const accessTo: Access = ({ req }) => {
      return {
        created_for: {
          contains: 1,
        },
      }
    }
  • default discord avatar
    mykz_last year

    looks fine.

  • default discord avatar
    notchrlast year

    its deff in the access fn



    i changed it to return true and the error went away

  • default discord avatar
    mykz_last year

    yeah



    I can do the same with my setup and then I don't get the Not Support error.



    super odd you get a different error though



    are you at beta.91 or higher?

  • default discord avatar
    notchrlast year

    I cancheck



    I think i setup this v3 instance like 2-3 weeks ago



    When i do this



    export const accessTo: Access = ({ data, id }) => {
      console.log(data, id)
      return true
    
    }


    I can see



    {
      title: 'Hello World',
      updatedAt: '2024-08-28T15:05:06.687Z',
      createdAt: '2024-08-28T15:05:06.687Z',
      created_for: [
        { relationTo: 'fruits', value: '66cf3a89ec8261afd0393ad3' },
        { relationTo: 'vegetables', value: '66cf3a93ec8261afd0393b35' }
      ]
    } 


    would a length check on

    data.created_for

    not be the same?

  • default discord avatar
    mykz_last year

    I wasn’t getting anything back from data when I tried



    Was undefined



    Yeah, I could just do the check on data, but how would that work on lists?



    Maybe the data is empty on a list view?

  • default discord avatar
    notchrlast year

    Hmm



    Well im not sure why i get like a bunch of undefined then the value





    Do you always get ID back

  • default discord avatar
    mykz_last year

    Only on a single view an id is available



    Will double check

  • default discord avatar
    notchrlast year

    wait



    Note: Field Access Controls does not support returning Query constraints like Collection Access Control does.

    Is the access supposed to limit document operations



    or specific to the field

  • default discord avatar
    mykz_last year

    For document



    The field is just so I know who has access to that document

  • default discord avatar
    notchrlast year

    Hmm



    So you didn't want to use roles?



    Like in your case fruits and vegetables are types of users?

  • default discord avatar
    mykz_last year

    it's a little more complicated from my side.



    I have users but they can have multiple memberships to say Partners, Clubs and Groups.



    then they would have roles for the membership they reside under if that make sense.

  • default discord avatar
    notchrlast year

    I see



    I was wondering if the admin access rule could maybe help



      access: {
        admin: ({ req: { user }}) => {
          return Boolean(user)
        },
      },


    which specifically restricts access to auth enabled collections

  • default discord avatar
    mykz_last year

    this just disables admin access and makes it api only right?

  • default discord avatar
    notchrlast year

    true

  • default discord avatar
    mykz_last year

    it's kinda like a multi-tenant app but with more than one tenancy if that make sense.



    Partners, Clubs and Groups instead of just a single tenancy.



    I might have to split the fields out into the different tenancies instead of having a single field I guess.

  • default discord avatar
    notchrlast year

    I can definitely brainstorm a bit



    Sorry I couldnt be more helpful 😄

  • default discord avatar
    mykz_last year

    you've been more than helpful man.



    it's just super complex system I'm trying to build and I'm not sure if Payload can support it maybe.

  • default discord avatar
    notchrlast year

    Always happy to help - I'll keep this open so someone else who may know more can add some info

  • default discord avatar
    mykz_last year

    sounds good man thanks!

  • default discord avatar
    notchrlast year

    I'm not the best with relational data stuff, but there are some folks here!

  • default discord avatar
    mykz_last year
    @1049775120559898725

    Solved it in the end and it was a syntax error like I thought.



    So it should be like this:


    {'field.value': { in: [1, 2, 3] }}
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.