Manual references not showing in Studio after client-side creation

5 replies
Last updated: Nov 21, 2020
Is there any reason why manual referencing documents via the client will not shop up on the studio?
1. i create a new document of type "A" then return the _id
2. I then create a document of type "B" and manually set the reference field with the _id of document "A"

   author: {
      _id: response._id,
      _type: 'reference'
    },
Technically that is correct, and when i inspect the data it is correct, but the selection in the studio is empty, afterwards, It does not allow me to manually select the referenced document within the studio, any ideas?
AI Update

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: true in your schema definition
  • After fixing your code to use _ref, existing documents with _id will 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 thread
5 replies
This is how my data and input looks before trying to select the correct document
and then if i try to select the document within the studio this happens
Replace the
_id
with
_ref
and it should work πŸ™‚
ah damn 🀦 thanks
haha πŸ‘

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?