How to Auto Populate References in Parent Document when New Children are Created
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:
Option 1: Query-Based Approach (Recommended)
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:
- Add a reference field in each child document pointing to its parent
- Install the @sanity/orderable-document-list plugin to enable drag-and-drop sorting
- Query children from your frontend using GROQ, ordered by the
orderRankfield 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:
- Write a function that listens for child document creation events
- Have the function identify the parent document (you'd need a reference field in the child)
- 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.
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.