Querying references and displaying them in Sanity Studio.

11 replies
Last updated: Aug 24, 2023
Do references store any parent data and if so how do i query it in groq ?
Aug 23, 2023, 7:28 PM
Can you clarify what you mean by parent data? An example to illustrate what you are trying to achieve would be helpful.
Aug 24, 2023, 9:12 AM
Of course! I have two documents, "Location Tags" and "Locations" i would like to add "Location Tags" to "Locations" and the reverse. However i am struggling with the schema. I created 3 separate "Location Tags" and i was able to reference them in "Locations" using this schema snippet:
  name: 'tags',
  title: 'Location Tags',
  type: 'array',
  of: [{ type: 'reference', to: { type: 'locationTag' }}],
Aug 24, 2023, 12:36 PM
This works fine, how ever there is no reference to these "Locations" when i look at the "Location Tags" in Sanity Studio. So i added a reference to "Locations" in the "Location Tag" document schema. Essential the reverse with this snippet:
  name: 'associatedLocation',
  title: 'Associated Locations',
  type: 'array',
  of: [{ type: 'reference', to: { type: 'location' } }],
Aug 24, 2023, 12:39 PM
When querying "Locations i get the location information but not the reference data.
"tags": *[_type == "locationTag" && references(^._id)]{
    "id": _id,
    "createdAt": _createdAt,
    "slug": slug.current,
Aug 24, 2023, 12:41 PM
Thanks for sharing additional information πŸ‘References are are
bi-directional by default and can be queried from either side of their relationship so you need not add the
bit.So if you want to get all locations by tag, you can query them like so:

*[_type == 'locationTag']{
  'locations': *[_type == 'location' && references(^._id)]{
If you want to get all tags by location, then you can query them as well like so:

*[_type == 'location']{
  'tags': tags[]->,
Aug 24, 2023, 12:59 PM
Thank you! I have been wracking my brain all day.
Aug 24, 2023, 1:15 PM
user F
last thing though, should these reference not also show up as UI elements in Sanity Studio? Currently my "location tag" document show no visual reference to it's relationship with "locations"
Aug 24, 2023, 1:17 PM
It will only show up in the "locations" document which is referencing these tags and not the other way round.
Aug 24, 2023, 1:18 PM
ok, and is there anything i can do to display this reference or is that just the way it work?
Aug 24, 2023, 1:19 PM
If a document was referenced in many different places then showing this in the UI would become unruly very quickly, so I presume this is the default behaviour that was chosen.If you do need to show it then you can try the method that you were experimenting with earlier to explicitly add a reverse reference I suppose.
The other option perhaps would be to build your own
Custom component which renders this list of locations for each tag.
Aug 24, 2023, 1:33 PM
Thank you, it's no the end of the world just needed clarification that it was "as designed" and not a bug in my own code. Have a great day and thank you again πŸ™‚
Aug 24, 2023, 1:40 PM

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?