How to Auto Populate References in Parent Document when New Children are Created

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

I understand what you're trying to achieve! You want parent documents to automatically have their reference arrays populated when child documents are created, while still allowing editors to manually sort those references. Let me walk through your options:

The Challenge with Auto-Populating References

Unfortunately, there isn't a built-in way to automatically populate a reference array in a parent document when a child is created. References in Sanity work in one direction—you need to explicitly add them where they're defined in your schema.

However, you have several approaches to solve this:

Instead of maintaining a reference array in the parent document, you can query for children on your frontend and let editors control the sort order through a plugin.

How it works:

  1. Add a reference field in each child document pointing to its parent
  2. Install the @sanity/orderable-document-list plugin to enable drag-and-drop sorting
  3. Query children from your frontend using GROQ, ordered by the orderRank field that the plugin manages

Example schema for child document:

{
  name: 'child',
  type: 'document',
  fields: [
    {
      name: 'parent',
      type: 'reference',
      to: [{type: 'parent'}]
    },
    // orderRank is managed by the orderable-document-list plugin
  ]
}

Example GROQ query:

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

Benefits:

  • No manual reference management needed
  • Editors can still reorder using drag-and-drop in the Studio
  • Simpler to maintain—no sync issues between parent and children
  • The orderable-document-list plugin provides an intuitive sorting interface

Option 2: Sanity Functions (More Complex)

You could use Sanity Functions to automatically update parent documents when children are created. Functions are serverless compute that run on Sanity's infrastructure and can react to document events.

How it would work:

  1. Write a function that listens for child document creation events
  2. Have the function identify the parent document (you'd need a reference field in the child)
  3. Use the Sanity Client within the function to add a reference to the new child in the parent's array

Important considerations:

  • This requires writing custom logic to identify parents and perform mutations
  • Functions are part of Sanity's Compute offering (requires Growth plan minimum)
  • Be careful about recursive loops—functions that trigger themselves
  • This adds significant complexity compared to the query-based approach
  • You'd still need the orderable-document-list plugin for sorting functionality

Option 3: Custom Input Component

You could build a custom input component that shows all potential children and lets editors select/sort them in one interface. This would still require manual selection but could make the UX much smoother than adding references one by one.

My Recommendation

Go with Option 1 (query-based approach with the orderable-document-list plugin). It's the simplest, most maintainable solution that still gives your editors full control over sorting. The "auto-population" happens naturally through the query—you're just flipping the relationship direction so children point to parents instead of parents maintaining arrays of children.

This is a common pattern in Sanity and works well because references are bidirectional when queried, even though they only appear in the Studio UI where they're defined. Your editors get a clean drag-and-drop sorting interface, and you avoid the complexity of keeping reference arrays in sync.

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?