Issue with parent/child taxonomy template in Sanity Studio v3

6 replies
Last updated: Jun 2, 2024
Hi all!I’ve been following User’s “Creating a Parent/Child Taxonomy” guide, up until the Pre-flight check section. I’ve copied the files exactly, yet when I try to access the documents I’ve created from the initial “All” folder, I get this error message:

Structure path

all26fc9c11-e7c1-42d6-92b8-d1b94cc3ce88category-child
Error

template id (`templateId`) is required for initial value template item nodes


I can access the documents if I go via one of the sub folders, which implies that the issue should be located at the bottom of the parentChild.ts file, here:


child: () =>

S.documentTypeList(schemaType)

.title('Children')
`.filter(
_type == $schemaType && parent._ref == $parentId
)`
.params({ schemaType, parentId: parent._id })

// Use this list for creating from child menu

.canHandleIntent((intentName, params) => intentName === 'create' && params.template === 'category-child')

.initialValueTemplates([

S.initialValueTemplateItem('category-child', {

parentId: parent._id,

}),

]),

I can't seem to find the error, everything looks fine to me. Has anyone else experienced this same issue? All help is greatly appreciated
🙏
AI Update

I can see the exact issue you're experiencing! Looking at the official Sanity Parent/Child Taxonomy guide and community discussions about this same error, the most common cause is actually a configuration placement issue rather than a problem with the S.initialValueTemplateItem() syntax itself.

The error "template id (templateId) is required for initial value template item nodes" typically occurs when the initial value template isn't properly registered or accessible when the Structure Builder runs.

The most likely culprit: Your templates configuration is placed outside the schema object in sanity.config.ts.

Here's what your config should look like:

// ./sanity.config.ts

import {defineConfig} from 'sanity'
import {schemaTypes} from './schemas'

export default defineConfig({
  // ...all other settings
  schema: {
    // All your schema types
    types: schemaTypes,
    
    // ⚠️ Make sure 'templates' is INSIDE the schema object
    templates: (prev) => {
      const categoryChild = {
        id: 'category-child',
        title: 'Category: Child',
        schemaType: 'category',
        parameters: [{name: `parentId`, title: `Parent ID`, type: `string`}],
        // This value will be passed-in from desk structure
        value: ({parentId}: {parentId: string}) => ({
          parent: {_type: 'reference', _ref: parentId},
        }),
      }
  
      return [...prev, categoryChild]
    },
  },
})

The key point: templates must be a property within the schema object, not a sibling to it.

Your Structure Builder code using S.initialValueTemplateItem('category-child', { parentId: parent._id }) is actually correct according to the official Parent/Child Taxonomy guide. The two-parameter syntax (template ID as first string parameter, parameters as second object parameter) is the documented and correct API for S.initialValueTemplateItem().

If moving the templates configuration inside the schema object doesn't solve it, double-check that:

  1. The template id ('category-child') matches exactly what you're referencing in the Structure Builder
  2. There are no typos in the template ID
  3. You've restarted your dev server after making config changes

This was exactly the issue in a community discussion where someone had the identical error - they confirmed: "I just realized what I did, a silly little mistake. I placed the templates outside of the schema object in sanity.config.ts 😛"

The reason you can access documents through the sub-folders is that those lists likely don't use the initialValueTemplates configuration, so they don't trigger this validation error. Once your template is properly registered within the schema object, the "All" folder should work correctly too!

For fun, I tried replacing initialValueTemplates with
.child(S.documentWithInitialValueTemplate('category-child', { parentId: parent._id })),
, which produces a slightly different error msg:
Error: Template with ID "category-child" not defined

Could it be that the structure builder is run before the templates function inside sanity.config.ts? Seems weird, tho...
👋 What version of the Studio are you running?
Oh wait, actually, have you defined the template, like so:
// ./sanity.config.ts

import {defineConfig} from 'sanity'
import {schemaTypes} from './schemas'

export default defineConfig({
  // ...all other settings
  schema: {
    // All your schema types
    types: schemaTypes,
    
    // Add this 'category child' template
    templates: (prev) => {
      const categoryChild = {
        id: 'category-child',
        title: 'Category: Child',
        schemaType: 'category',
        parameters: [{name: `parentId`, title: `Parent ID`, type: `string`}],
        // This value will be passed-in from desk structure
        value: ({parentId}: {parentId: string}) => ({
          parent: {_type: 'reference', _ref: parentId},
        }),
      }
  
      return [...prev, categoryChild]
    },
  },
That's a good guess as to why it wouldn't have worked, but I have indeed added the template.
And it's v3 of the studio, the project was started no more than a month ago.
I just realized what I did, a silly little mistake. I placed the templates outside of the schema object in sanity.config.ts 😛

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?