Can you reference a document in draft mode when creating a new document?

6 replies
Last updated: Apr 8, 2024
Is it possible to reference a document in in "draft mode" when creating a new document (also in draft mode) ? Currently attempting to create a structure that visually represent "sub-pages" but creating a new "sub-page" for a "parent" in "draft-mode" i get this error as soon as the auto-save kicks in:
AI Update

Yes! You can absolutely reference documents in draft mode from other draft documents using weak references. This is exactly the use case weak references were designed for.

By default, Sanity uses "strong" references that only point to published documents. When you try to reference a draft document with a standard reference, you'll get validation errors on auto-save because the published version doesn't exist yet.

The Solution: Add weak: true to Your Reference Field

In your schema definition, modify your reference field to include weak: true:

{
  name: 'parentPage',
  type: 'reference',
  to: [{type: 'page'}],
  weak: true  // This allows referencing draft documents
}

This tells Sanity that the reference doesn't need to point to a published document, so you can reference drafts without errors.

How It Works

  • Strong references (default) require the referenced document to be published and enforce referential integrity
  • Weak references allow you to reference documents regardless of their publication status—draft, published, or even non-existent

This is perfect for your sub-page/parent structure where you want to create child pages that reference a parent page that's still in draft mode.

Important Notes

For Programmatic Creation

If you're creating documents through mutations or the client API, use _weak (with an underscore) instead:

{
  _type: 'reference',
  _ref: 'parent-document-id',
  _weak: true
}

For Initial Values

When setting initial values that include references to drafts, make sure to include _weak: true in the reference object.

Querying Considerations

When querying with GROQ, the standard -> dereferencing operator only fetches published documents. If you need to retrieve draft versions of weakly-referenced documents, you may need to write custom queries that handle both drafts.* and published IDs.

Weak references give you the workflow flexibility to build interconnected content structures without forcing a specific publishing order, which is exactly what you need for your parent/sub-page hierarchy! You can read more about this in the Sanity reference type documentation.

You would need to use a
weak
reference to be able to reference unpublished documents.
I tried this, mind you i may have implemented it wrong, so i will try again, but just to be clear, can you have a weak reference to a draft, from a document that does not yet exist? And by that i mean is still in the initial phase before autosave has been triggered?
Yes!
Fantastic!
Thank you
Just FYI, we solved the problem by adding
_weak: true
to initial values that contain references. This is essential if you want to be able to maintain references in draft mode.

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?