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
notchr3 months ago
1

Hmm



Looking into it



Though, if you got the

Not supported

error, it may be



Seems like it should be though



This is postgres?



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



interesting



That's the payload error?



Might be from here

https://github.com/payloadcms/payload/blob/61da01099144fcefee7bec98e631e8a964179b76/packages/db-postgres/src/queries/getTableColumnFromPath.ts#L529

Doesn't seem right though



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



Whats the access function look like



hmm



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



We don't know which condition failed though



It does seem odd that's not available



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



hmm



maybe it's just contains?



Would like..



equals work?



and how about

in

in: [1, 2, 3]



hmm



Based on those conditions?



This is on v2 right?



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



ok i did





like this



then enable access?



for read?



Error: formattedValue.replace is not a function



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',
    },
  ],
}


import type { Access } from 'payload'

export const accessTo: Access = ({ req }) => {
  return {
    created_for: {
      contains: 1,
    },
  }
}


its deff in the access fn



i changed it to return true and the error went away



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?



Hmm



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





Do you always get ID back



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



Hmm



So you didn't want to use roles?



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



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



true



I can definitely brainstorm a bit



Sorry I couldnt be more helpful 😄



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



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

  • default discord avatar
    mykz_4 months ago

    Really appreciate it!



    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,
     }
    },


    yeah, postgres



    Error: Not supported

    gives me 500 error

    /admin/collections/clubs?limit=10

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



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



    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.





    yeah



    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.



    yeah



    why it makes me think my query syntax is wrong.



    no, same error for equals



    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.



    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





    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,
    },


    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.



    yeah



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



    odd



    whats your accessTo look like?



    looks fine.



    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?



    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?



    Only on a single view an id is available



    Will double check



    For document



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



    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.



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



    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.



    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.



    sounds good man thanks!



    @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.