🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Discussing linking embedded documents in Sanity.io schemas

9 replies
Last updated: Nov 15, 2021
I have a document of type
Parent
that has a
content
field that is an array of documents of type
Child
(not reference to
Child
). I would like to create another field that can choose one
Child
from the `Parent`'s
content
field. Is this possible?
Nov 12, 2021, 2:05 PM
Hey User! I'm curious, if the
Child
documents aren't references, what does this look like in your schema? Re: selecting a
Child
, what's the ultimate goal of selecting it?
Nov 12, 2021, 4:48 PM
const lesson = {
  type: 'document',
  name: 'lesson',
  title: 'Lesson',
  fields: [
    {
      type: 'array',
      name: 'content',
      title: 'Content',
      of: [
        {
          type: 'section',
        }
      ],
    },
  ],
}

const module = {
  type: 'document',
  name: 'module',
  title: 'Module',
  fields: [
    {
      type: 'array',
      name: 'content',
      title: 'Content',
      sortable: true,
      of: [
        {
          type: 'lesson',
        },
      ],
    },
  ],
}
Nov 12, 2021, 5:29 PM
user M
simplified, but I want to be able to refer to a
lesson
that is in a `module`'s
content
array
Nov 12, 2021, 5:30 PM
It looks like there is no way to do this in vanilla Sanity (please tell me I'm wrong) and I'm looking at custom input components. However, the
props
passed to
React.forwardRef
don't seem to provide a means to access the
parent
object / document (or at least, I can't seem to find a way).
Nov 12, 2021, 5:35 PM
My use case is that I want to be able to refer to one
lesson
from another
lesson
. I was doing this with references and a custom filter, which worked when the
lesson
was a separate document and referenced by the module. Now that it is embedded in the module, I need another way of referencing / selecting the
lesson
.
Nov 12, 2021, 5:50 PM
The reason that we have embedded the `lesson`s inside the
module
(and
module
in
course
, and
course
in
programme
) is that there were simply too many loose documents, and the course creators where having to jump through hoops to manage the documents. Now they all exist within the context of a single
programme
container, everything is much simpler - except I still need to link from one
lesson
to another.
Nov 12, 2021, 5:53 PM
If you're trying to create references between lessons, I recommend you create them as documents. As you mentioned, you could create a custom input component that queries for information in a field in another document, but this is a really brittle content model. I think you'll find it's easier to maintain if you create them as documents, use dynamic filters in your reference fields and create a custom desk structure for organizing the documents.
However, if you do decide to go the custom input component route, you can access other fields in the document by exporting the component with the
withDocument
HOC .
Nov 12, 2021, 8:14 PM
user M
we've gone back to using references, and whilst I can link (refer) to lessons, I will need solve the organisation of content in the desk structure and programme publishing (all linked documents need to be published too). But that is for another day... :-)
Nov 15, 2021, 8:08 AM
Working with Structure Builder can be a bit daunting when you first start working with it, so feel free to come back if you get stuck!
Nov 15, 2021, 6:15 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?