How to Query relationship field

default discord avatar
jbalatero2 years ago
1 3

Hello, I have a customer collection with auth: true and a bills collection

When a customer is authenticated, I wanted to be able to query only its own bills. I am however confused - getting empty bills from the api using api key.

I already have entries of bills assigned to specific customer but still empty response.

bills collection fields simplified

{
	access: {
		read: ({req: {user}}) => {
            if (user) {
                return {
                    customer_id: { 
                        equals: user.id,
                    },
                };
            }

            return false;
        },
	},
	fields: [
           {
              label: 'Customer',
              name: 'customer_id',
              type: 'relationship',
              relationTo: ['customers'],
              required: true,
              index: true,
            },
	],
}
  • Selected Answer
    discord user avatar
    jmikrut
    2 years ago

    Hey @eudora-fabia — you're close to being there!

    So, your customer_id field is actually set up with an array of relations:

    	fields: [
               {
                  label: 'Customer',
                  name: 'customer_id',
                  type: 'relationship',
                  relationTo: ['customers'], // set up for MANY relations
                  required: true,
                  index: true,
                },
    	],

    Setting your relationTo as an array, as you've done, will create the following field data structure in your database:

    customer_id: {
      relationTo: 'customers',
      value: '609490aa305c5c6a913d30bc',
    },

    When you have multiple relationTos, we also need to store the collection slug that contains the related document to the database. So that means, in your case, to query for all bills that are owned by a specific customer, you'd need to use customer_id.value. Like this, in the REST API:

    ?where[customer_id.value][equals]=609490aa305c5c6a913d30bc // notice the `.value`
    

    Now, in contrast, I suspect that your customer_id field really only needs one relationTo. If your field was set up to take only ONE relationTo, like this:

    	fields: [
               {
                  label: 'Customer',
                  name: 'customer_id',
                  type: 'relationship',
                  relationTo: 'customers', // set up for ONE relation
                  required: true,
                  index: true,
                },
    	],

    The data would be saved in a simpler format in the database, because we know that the related collection is always customers:

    customer_id: '609490aa305c5c6a913d30bc',

    So, to query on this data, you'd do:

    ?where[customer_id][equals]=609490aa305c5c6a913d30bc
    

    Does this answer your question?

    1 reply
  • default discord avatar
    jbalatero2 years ago

    Wow it worked now! thank you very much.

  • default discord avatar
    richardvanbergen2 years ago

    Now, in contrast, I suspect that your customer_id field really only needs one relationTo. If your field was set up to take only ONE relationTo, like this:

    This is something I came upon the other day. Would be nice to make it clearer that there is a significant difference between ['relation'] and 'relation'.

    2 replies
    discord user avatar
    jmikrut
    2 years ago

    Good call. We'll beef up the docs regarding this as I agree it's important and can be missed easily.

    default discord avatar
    Ontopic2 years ago

    Same 😅
    I went crawling through my codebase after reading this to see where I used [] in anticipation of maybe adding relationships later, not realising it had an impact

  • discord user avatar
    jmikrut
    2 years ago

    where I used [] in anticipation of maybe adding relationships later

    This is actually a great call. If you are ever planning to add more relationships, or even a remote possibility exists that you might, it'll be easier over the longer term to just start out with [ 'collection-name' ] instead. That way you wouldn't need to migrate your data when / if you needed to support more collection relationships.

    This is about to go in the docs for sure!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.