Auto-populate reference array based on reverse relationship in Sanity

2 replies
Last updated: May 13, 2020
Hey all, I’m trying to create an array of references in a product category document that populates with products that have their category defined. So, if I have a type of window with a category of “vinyl window”, I’d like that to show up in the array of refs in my “vinyl window” category. I found the validation section in the docs, but I’m not sure how to implement the idea
AI Update

Based on what you're describing, it sounds like you want to automatically show which products belong to a category, rather than manually maintaining an array of references. The good news is you don't need to store this in your category document at all - you can use GROQ's references() function to dynamically find all products that reference a specific category!

The Schema Setup

First, make sure your product schema has a reference to the category:

// product.js
{
  name: 'product',
  type: 'document',
  fields: [
    {
      name: 'title',
      type: 'string'
    },
    {
      name: 'category',
      type: 'reference',
      to: [{type: 'productCategory'}]
    }
    // other fields...
  ]
}

Querying Products by Category

Instead of maintaining an array in the category document, you can query for products that reference the category using the references() function:

*[_type == "productCategory" && slug.current == "vinyl-window"][0]{
  _id,
  title,
  "products": *[_type == "product" && references(^._id)]{
    title,
    price,
    // other fields you want
  }
}

The key here is references(^._id) - this finds all products that contain a reference to the current category's ID. The ^ operator accesses the parent document's ID within the subquery.

Why This Approach is Better

Rather than storing an array of references in your category document (which would require manual maintenance), this approach:

  • Automatically shows all products in a category based on the product's category field
  • Maintains a single source of truth (the product's category field)
  • Avoids data duplication and sync issues
  • Makes it easier to change a product's category

If You Really Need the Array in Studio

If you specifically want to see this list in the Studio UI when editing a category, you could create a custom component that displays the products, but the data itself shouldn't be stored as an editable array - it should be computed from the references.

The validation section you found is for validating user input, not for auto-populating fields based on relationships. The references() function in GROQ is the right tool for what you're trying to accomplish!

Show original thread
2 replies
So, you're kind of trying to show incoming references for a document. E.g. "all documents referencing this document", right?
There's no out of the box way of doing that, but one way would be to create a new document tab and query for the documents in that tab and list them out yourself... would that work for you?


https://www.sanity.io/docs/structure-builder-typical-use-cases#tabs-with-content-previews-a8e5cc70dbc0
So, you're kind of trying to show incoming references for a document. E.g. "all documents referencing this document", right?
There's no out of the box way of doing that, but one way would be to create a new document tab and query for the documents in that tab and list them out yourself... would that work for you?


https://www.sanity.io/docs/structure-builder-typical-use-cases#tabs-with-content-previews-a8e5cc70dbc0

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?