Auto-populating array of references to child documents in Sanity

1 replies
Last updated: Apr 16, 2020
Hi, i have two documents. One document is a parent and the other is children of the first. In the parent document i want an array with all the referances to the children to be auto populated when new children is created. This is so that the end user can sort the documents in what ever order they see fit but also so that they wont have to manually add all the children manualy. Is this possible and/or is there any other way for the enduser to sort a list in whatever way they want? 🙂
AI Update

Based on what you're describing, there are a couple of approaches to solve this problem, though auto-populating parent references isn't the typical pattern in Sanity.

Instead of auto-populating references in the parent document, I'd recommend structuring your schema so that children reference their parent. Then you can query all children for a given parent using GROQ. This is the most common pattern because:

  1. Children naturally "know" their parent
  2. You avoid the complexity of auto-population
  3. Users can still manually sort the children

Here's how it works:

Child schema:

{
  name: 'child',
  type: 'document',
  fields: [
    {
      name: 'parent',
      type: 'reference',
      to: [{type: 'parent'}]
    },
    {
      name: 'orderRank',
      type: 'string',
      hidden: true
    }
    // other fields
  ]
}

Query to get sorted children:

*[_type == "parent" && _id == $parentId][0] {
  ...,
  "children": *[_type == "child" && parent._ref == ^._id] | order(orderRank)
}

For manual sorting, use the Orderable Document List plugin which provides drag-and-drop ordering functionality in Studio using the orderRank field.

Auto-Population with Sanity Functions

If you really need the parent to contain an array of child references, you can use Sanity Functions to automatically update the parent when children are created. Functions are Sanity's modern, serverless compute solution that runs directly on Sanity's infrastructure - no external hosting needed.

You'd set up a Function that:

  1. Listens for document create/delete events on child documents
  2. Updates the parent's reference array accordingly
  3. Maintains the orderRank for sorting

However, this approach has downsides:

  • More complexity
  • Potential for race conditions
  • You need to handle edge cases (what if the function fails?)

The Array-in-Parent Approach

A third option is to manually manage references in the parent, but make it less tedious:

{
  name: 'parent',
  type: 'document',
  fields: [
    {
      name: 'children',
      type: 'array',
      of: [{
        type: 'reference',
        to: [{type: 'child'}]
      }]
    }
  ]
}

The array itself is drag-and-drop sortable in Studio, so users can reorder as needed. While they need to manually add children, this gives them complete control over which children appear and in what order.

For your use case, I'd strongly recommend the reverse reference + Orderable Document List approach - it's cleaner, more maintainable, and gives users the sorting control they need without manual reference management.

Show original thread
1 reply
I did something like this. You can use the query listeners: https://www.sanity.io/docs/listening and then do a "patch" https://www.sanity.io/docs/http-patches

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?