Document References - No Results for Document Type, Changed from Object

7 replies
Last updated: Mar 15, 2021
I am trying to get a document to refrence other documents of the same type. But I only get "No results" when trying to search for instances of that document type.
The document type was an object up until an hour ago ish, I changed it to a document type because it seems like refrences only works for documents, Then I crated some new instances of that document but cannot find them either.

https://pastebin.com/Fgx3YRP3
Any ideas?
🙂
AI Update

I can see the issue in your schema! Looking at the pastebin, on lines 11-22, you have a duplicate type definition that's causing the problem:

{
  type: "string",  // ← Line 12: This is wrong!
  title: "Related success criterion",
  name: "accessibilityRelatedSuccessCriterion",
  type: "array",   // ← Line 14: This gets overridden by line 12
  of: [
    {
      type: "reference",
      to: [{ type: "successCriterion" }],
    },
  ],
},

The fix: Remove the type: "string" on line 12. It should be:

{
  title: "Related success criterion",
  name: "accessibilityRelatedSuccessCriterion",
  type: "array",
  of: [
    {
      type: "reference",
      to: [{ type: "successCriterion" }],
    },
  ],
},

Why you're getting "No results":

  1. The duplicate type definition was making the field a string instead of an array of references, breaking the reference functionality
  2. Schema needs to reload - After changing from object to document type, you need to restart your Studio
  3. Documents must be published - References typically only show published documents by default, not drafts

Steps to fix:

  1. Correct the schema by removing the duplicate type: "string" line
  2. Restart your Sanity Studio completely (stop and restart sanity dev)
  3. Make sure you've published (not just saved as drafts) some successCriterion documents
  4. Try the reference field again - the search should now work!

You were absolutely right that references only work with document types (not objects), so that change was necessary. Once you fix the duplicate type and restart, everything should work as expected! 🙂

Are you using the
intl-input
plugin?
no
Alright, then I can’t help you 😕
I would expect the documents that were formerly objects to be a bit tougher to get at, but you mentioned new instances that you can’t find either. What do you get when you query:

*[_type == "successCriterion"]
Are you querying in Vision or some other tool?
I would expect the documents that were formerly objects
I didn’t word that well. They’re still objects and won’t automatically update to documents just because the schema is changed.
Creating new documents from the root of sanity worked, but just adding them via into an array object in sanity did not work. But that is actually fine, this is how I wanted it to work any way. I was just prototyping
thanks
user A
and
user R
❤️

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?