Manual references not showing in Studio after client-side creation
The issue you're experiencing is due to an incorrect reference structure when creating documents programmatically. You're using _id when you should be using _ref.
The Problem
Your current code:
author: {
_id: response._id, // β Wrong property name
_type: 'reference'
}This creates malformed reference data that Sanity Studio cannot recognize, which is why the selection appears empty and you can't select the referenced document.
The Solution
The correct structure for a reference field is:
author: {
_type: 'reference',
_ref: response._id // β
Correct - use _ref, not _id
}Complete Example
Here's the proper way to create documents with references:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset-name',
useCdn: false,
apiVersion: '2023-05-03',
token: process.env.SANITY_SECRET_TOKEN
})
// 1. Create document A
const docA = await client.create({
_type: 'A',
// ... other fields
})
// 2. Create document B with reference to A
const docB = await client.create({
_type: 'B',
author: {
_type: 'reference',
_ref: docA._id // Use _ref property
}
})Why This Matters
According to the Sanity reference type documentation, a reference field must have:
_type: 'reference'- identifies this as a reference type_ref: 'document-id'- contains the ID of the referenced document
The _ref property is what Sanity uses to establish the relationship between documents. Using _id instead creates invalid reference data that Studio cannot interpret.
Important Notes
- Always use the published document ID when creating references (without the
drafts.prefix) - For strong references (the default), the referenced document must exist before you create the reference
- If you need to reference documents that may not exist yet, set
weak: truein your schema definition - After fixing your code to use
_ref, existing documents with_idwill need to be corrected - you can do this with a migration
Once you update your code to use _ref instead of _id, Studio will properly recognize the references and allow you to view and select the referenced documents.
Show original thread5 replies
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.