Reference type is missing or has an invalid value for the "to" property
Sanity Studio reports The reference type is missing or having an invalid value for the required "to" property. It should be an array of accepted types. when a reference type has no to, or when to is neither an array nor an object.
All reference types must define what type of documents they may refer to. The to property should be an array of objects, where each object describes the type of a valid reference. Each entry in to must have a type property, which must be the name of a valid schema type. A single object is also accepted and normalized to a one-item array.
An empty to array reports a different message: The reference type should define at least one accepted type. Please check the "to" property.
import {defineField} from 'sanity'
export const authorReference = defineField({
type: 'reference',
name: 'authorReference',
// The "to" property must be set, and it must list at least one type
to: [
{type: 'author'}, // type is required
{type: 'book'},
],
})Reference types must be unique, or named
Sanity Studio reports Found 2 members with same type, but not unique names "author" in reference. This makes it impossible to tell their values apart and you should consider naming them when two entries share a type and neither is named.
In order to know which type description a reference value belongs to, you can't add multiple entries to to with the same type unless you also give them distinct name values. This is therefore not allowed:
import {defineField} from 'sanity'
export const authorReference = defineField({
type: 'reference',
name: 'authorReference',
to: [
{type: 'author'},
// 💥 ERROR: no way to tell the two entries apart
{type: 'author'},
],
})Instead, you can give one of the entries a name. This satisfies the schema validator:
import {defineField} from 'sanity'
export const authorReference = defineField({
type: 'reference',
name: 'authorReference',
to: [
{type: 'author', title: 'Author'},
{type: 'author', name: 'anotherAuthorReference', title: 'Another author'},
],
})Naming a duplicate entry satisfies the schema validator, but it does not let you tell the reference values apart. A reference value stores the name of the reference type itself, not the entry that was selected. For a field declared inline with type: 'reference', that value is {"_type": "reference", "_ref": "329e893ewi"}. If you declare the reference as a named top-level type, _type is that type's name instead. Sanity Studio decides which to entry applies by matching the referenced document's own _type, so a named entry never matches and the unnamed entry absorbs every reference of that type. When two entries point at the same type, removing the duplicate is usually the better fix.