Updating schema and associated data in Sanity Studio

22 replies
Last updated: Jan 17, 2022
I've updated my schema within the github browser page (didn't have a copy locally), what do I need to do for the new schema to be used? I published it to the main branch.
Jan 10, 2022, 7:31 PM
I am accessing the deployed studio, then I realized I needed an additional piece of data - so I added it to the schema within the files on git. Am I understanding that I need to clone so that I can redeploy?
Jan 10, 2022, 7:36 PM
I am accessing the deployed studio, then I realized I needed an additional piece of data - so I added it to the schema within the files on git. Am I understanding that I need to clone so that I can redeploy?
Jan 10, 2022, 7:36 PM
Correct.
Jan 10, 2022, 7:38 PM
Thanks! What about all of the data I've created so far – it will still remain, right? I'm not removing any data pieces from the schema.
Jan 10, 2022, 7:39 PM
Correct! Your dataset is hosted separately from the Studio, so you can't accidentally overwrite it by redeploying the Studio.
Jan 10, 2022, 7:40 PM
perfect, thx
Jan 10, 2022, 7:46 PM
user M
I changed my schema, which included the exact name of the document types. Now all of my data is gone! Is there a way to change the schema and have the associated data update with it?
Jan 13, 2022, 4:40 PM
user M
I changed my schema, which included the exact name of the document types. Now all of my data is gone! Is there a way to change the schema and have the associated data update with it?
Jan 13, 2022, 4:40 PM
So, if you change your schema the content you've already created doesn't automatically update. It still exists, though! Your Studio just doesn't know how to display it since the schema changed. You'll need to perform a mutation on your content to update it. I prefer to use the JS client to write a script in cases like this.
Jan 13, 2022, 4:45 PM
Great! I knew you'd have a solution. is this the part of the page I need to pay attention to?
Jan 13, 2022, 4:50 PM
I'd use
patch
then
set
to alter existing docs?
Jan 13, 2022, 4:51 PM
Exactly, as well as
unset
if you need to remove a field.
Jan 13, 2022, 4:53 PM
Exactly, as well as
unset
if you need to remove a field.
Jan 13, 2022, 4:53 PM
perfect. thanks again!
Jan 13, 2022, 4:55 PM
one more question (and it feels like a dumb one): I'm not seeing in the docs about the JS client where I put this code, and how I run it. Do I create the script, add it to my HTML file, then load the page once to mutate the data?
Jan 13, 2022, 5:01 PM
Also, does the file need to be within the sanity folder to have access to the JS client I installed in there? Or should I have installed it in the (parent) project folder where my JS/HTML already exists?
Jan 13, 2022, 5:02 PM
user M
just checking if you saw this. Not to bug you too much...😬
Jan 13, 2022, 6:51 PM
I usually put it in my studio folder. Then you can run the script using
sanity exec <script-name-here> --withUserToken
Jan 13, 2022, 8:24 PM
Cool, thanks!! Is that somewhere in the documentation? If not, that should probably be added.
Jan 13, 2022, 8:34 PM
Thanks for the feedback! You can find info that particular command here . But it feels like a situation where you would have to already know what you're looking for. Someone new might not these things.
Jan 13, 2022, 8:44 PM
Ahh, yeah - I'd really recommend putting a link to this on the first page you sent me, since it's necessary to actually use the info on that first page. πŸ˜‡
Thanks again!!
Jan 13, 2022, 8:48 PM
Actually, all of this JS client documentation seems like it's missing some bits of info, especially where the syntax is less similar to vanilla JS.
To double check: will this code rename all docs with _type: 'senator' into 'senators'? Do I need to add anything, or did I misunderstand how to interpret the docs?


const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: '6dyl9k59',
  dataset: 'production',
  apiVersion: '2022-01-01', // use current UTC date - see "specifying API version"!
  token: 'skiq9pnWYIvHOlYZLz4P7Zg5dhl1zWhnq3Fc6GrtVnKTiy28SXm8XG0jTjD5KABRxh2jigsd418SuIjFbGwJDELksxZYkaTbCCgXkidnKazetbkBnUypEuIgIaZ3w3HHp8aXO6HlScrHB3mzzEHCS4Rk03MTUTsrbTTebWADYmLeeeFoAXRP', // or leave blank for unauthenticated usage
  useCdn: false, // `false` if you want to ensure fresh data
})


const namePatch = client.patch({ query: '*[_type == $type]', params: { type: 'senator' } }).set({_type: 'senators'});

namePatch();
Jan 17, 2022, 4:02 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?