Is `_key` unique across document type or just within a document?

2 replies
Last updated: Apr 29, 2021
Does anyone know if the
_key
of an array item is unique across the document type (as opposed to the document)? https://www.sanity.io/docs/array-type#92296c6c45ea
AI Update

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 _key must be unique among items in that specific array
  • Across arrays in the same document: Different arrays can have items with the same _key value
  • Across different documents: Multiple documents can have array items with identical _key values

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 thread
2 replies
The
_key
only needs to be unique within the array itself, so I imagine that’s the way Sanity implements key assignment, too.
Ah I see, that makes sense, thanks!

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?