Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Hi, anyone able to help with a relationship thing? I have several schemas set up, like, Country, County, and Business, each country has counties (county) each county has...

2 replies
Last updated: Dec 29, 2020
Hi, anyone able to help with a relationship thing?
I have several schemas set up, like, Country, County, and Business, each country has counties (county) each county has businesses, so, each business belongs to one county and I want to be able to GROQ all businesses in a county.

I have made a query for all counties in a country where I add a county to a country then I can get the county names from the country *`*[_type == "country"]{...,county[]->}`* but I don't want the user to have to add the business to a county as logically when you're entering the address details for a business in the CMS you'd do it all in on data entry form, I guess... If it's not all in one place then the user will probably forget to add the business to the county then there'll be no record of it when searching for businesses by county.

What I trying to articulate (badly) is that I want to do the reverse for relationships so I can get all businesses in a county by adding the county to the business rather than the business to the county
Dec 28, 2020, 8:11 PM
You should be able to do that with something like this:
*[type == "county"]{
  ...,
  "businesses": *[_type=="business" && references(^._id)]{
    ...
  }
}

Dec 28, 2020, 8:41 PM
Brilliant, thanks
Cory Gibbons
🙏
Dec 29, 2020, 5:44 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?