Best practices for backfilling content in Sanity when adding new fields to schema.
6 replies
Last updated: Jun 19, 2023
S
Hello everyone, I am fairly new to Sanity and was wondering if there is a native solution or best practice for backfilling content when a new field is added to the schema or if one of the existing fields changes. For example, lets say I had a blog schema with content already created but I want to add a subtitle field, how would I backfill all the existing content to have the subtitle field?
Jun 19, 2023, 8:38 PM
Hi
user A
. In some cases you might use a script to backfill existing documents, but I’m assuming subtitle will be fairly specific to each blog post so you might need a more manual approach. Do these subtitles already exist in your data outside of Sanity somewhere?Jun 19, 2023, 8:41 PM
S
user A
In this example the subtitle would not exist outside of Sanity we would be creating them on the flyJun 19, 2023, 8:44 PM
In that case, once you’ve added the subtitle field to your schema, you’ll probably end up iterating over your blog posts in your Studio, adding the subtitle to each document. If it helps, you can add the subtitle field as the first item in your schema so that it gets focus when you open each document, then once you’re done you can move it where you want it to live in your schema file (the Content Lake doesn’t care).
An alternative approach would be to export your data, add all the subtitles to the NDJSON file, and then import that back into Sanity. This lets you work in a single file, and depending on the editor you’re using you may be able to take advantage of macros or repeatable actions to streamline this.
An alternative approach would be to export your data, add all the subtitles to the NDJSON file, and then import that back into Sanity. This lets you work in a single file, and depending on the editor you’re using you may be able to take advantage of macros or repeatable actions to streamline this.
Jun 19, 2023, 8:48 PM
S
Ok that confirms our understanding of backfilling in the sanity environment. What if I added a default value for the subtitle? would all the existing documents get a subtitle with the default value? Is this a good practice?
Jun 19, 2023, 8:51 PM
If you mean an initial value, that can only be done when the document is initially created. Existing documents won’t be impacted.
I think leaving your
I think leaving your
subtitleempty in the meantime (it’s worth noting that in your Content Lake, the field will actually be undefined) is fine, so long as you account for that where you’re querying your data (e.g., your front end) with appropriate null checking or something similar.
Jun 19, 2023, 8:53 PM
S
user A
Ok thank you for the help!Jun 19, 2023, 8:56 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.