Is `_key` unique across document type or just within a document?
Based on the documentation, _key is unique only within its containing array, not across the entire document type.
The documentation on _key specifically states that "_key values only need to be unique within their containing array." This is different from document IDs (_id), which must be unique across your entire dataset.
So to clarify the scope of uniqueness:
- Within an array: Each
_keymust be unique among items in that specific array - Across arrays in the same document: Different arrays can have items with the same
_keyvalue - Across different documents: Multiple documents can have array items with identical
_keyvalues
For example, this is perfectly valid:
{
_id: "doc1",
arrayA: [
{ _key: "abc123", title: "Item 1" },
{ _key: "def456", title: "Item 2" }
],
arrayB: [
{ _key: "abc123", title: "Different Item" } // Same _key as in arrayA - totally fine!
]
}When you're creating array items programmatically, you just need to ensure uniqueness within that specific array. Using a UUID generator (like the uuid package) is the standard approach to avoid collisions within the array you're working with.
Show original thread2 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.