Unlock seamless workflows and faster delivery with our latest releases – get the details

Linking coffee shops to amenities using array of references in Sanity.io

3 replies
Last updated: Jun 27, 2024
Hey Friends, I'm looking for a way to reference one document to many documents. My example is one coffee shop (table 1) to having many amenities (table 2)
"Table one: coffee shops
Table Two: Amenities (wifi, bathrooms)

I want to link that this coffee shop has 2 amenities(via referencing the IDs of two rows in the Amenities table. How do I do this?"

Here's an image of how a joins table would do this.

I read this page but didn't deliver.


https://www.sanity.io/docs/reference-type
Jun 26, 2024, 4:39 AM
Sounds like you can use an array of references? Something like:

{
  title: “Amenities”,
  name: “amenities”,
  type: “array”,
  of: [
    {
      type: “reference”, 
      to: [
        {type: “amenity”}
      ]
   }
 ]
}
Jun 26, 2024, 4:50 AM
Rad I'll give this a try!
Jun 26, 2024, 5:41 AM
This worked thank you.
note: " " double quotes created an error.

` ` single quotes make it work.

defineField({          
            name: 'amenities',
            title: 'Amenities',
            type: 'array',
            of: [
                {
                type: 'reference', 
                to: [
                    {type: 'amenity'}
                    ]
                }
             ]
        }),
Jun 27, 2024, 5:12 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?