Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with creating IDs in Sanity.io data import

9 replies
Last updated: Mar 12, 2021
Odd one. I’m doing a data import and auto-generating ID’s so I can create ref’s on the fly.
This returns a bad request

[ { createOrReplace: { _type: "team", _id: "team-67" } } ]
This imported fine (underscore in
_id
string)
[ { createOrReplace: { _type: "team", _id: "team_67" } } ]
Stranger still, it didn’t have the same issues with
teamMember-123
or
office-456
, only on this
team-
type.
(This is via the HTTP mutations API)
Mar 4, 2021, 7:26 AM
What was the error message? Could it be that you have a document with that ID already, but it has a different
_type
?
Mar 4, 2021, 6:20 PM
Brand new dataset. Error was 400 bad request ... but I might not have been logging the full error.
Mar 4, 2021, 6:28 PM
{
  error: {
    description: 'The mutation(s) failed: Cannot modify immutable attribute "_type"',
    items: [ { error: [Object], index: 10 } ],
    type: "mutationError"
  }
}
Mar 4, 2021, 6:50 PM
Just emptied the dataset and tried again. Confirmed, ID’s of
team-123
give this error. ID’s with
team_123
work.
Mar 4, 2021, 6:51 PM
Are you sure there isn't another document being created in your script/import that is using the same ID? The immutable attribute error happens when you try to change the
_type
attribute for a document that aleady exists (even through
createOrReplace
)
Mar 4, 2021, 9:33 PM
Yeah it’s possible my import is screwy! So, for prosperity I’ve just dumped out all the types and id’s that are being sent to be created… I don’t see how it’s importing the same ID in two types.
I have solved this for now by just using underscores.


[
    { _type: "office", _id: "office_roma" },
    { _type: "office", _id: "office_stanthorpe" },
    { _type: "office", _id: "office_toowoomba" },
    { _type: "team", _id: "team_39" },
    { _type: "team", _id: "team_40" },
    { _type: "team", _id: "team_41" },
    { _type: "team", _id: "team_42" },
    { _type: "team", _id: "team_43" },
    { _type: "team", _id: "team_44" },
    { _type: "team", _id: "team_45" },
    { _type: "team", _id: "team_46" },
    { _type: "team", _id: "team_47" },
    { _type: "team", _id: "team_48" },
    { _type: "team", _id: "team_49" },
    { _type: "team", _id: "team_57" },
    { _type: "team", _id: "team_58" },
    { _type: "team", _id: "team_64" },
    { _type: "team", _id: "team_67" },
    { _type: "teamMember", _id: "teamMember_1" },
    { _type: "teamMember", _id: "teamMember_108" },
    { _type: "teamMember", _id: "teamMember_11" },
    { _type: "teamMember", _id: "teamMember_110" },
    { _type: "teamMember", _id: "teamMember_111" },
    { _type: "teamMember", _id: "teamMember_114" },
    { _type: "teamMember", _id: "teamMember_115" },
    { _type: "teamMember", _id: "teamMember_121" },
    { _type: "teamMember", _id: "teamMember_123" },
    { _type: "teamMember", _id: "teamMember_124" },
    { _type: "teamMember", _id: "teamMember_130" },
    { _type: "teamMember", _id: "teamMember_132" },
    { _type: "teamMember", _id: "teamMember_136" },
    { _type: "teamMember", _id: "teamMember_137" },
    { _type: "teamMember", _id: "teamMember_138" },
    { _type: "teamMember", _id: "teamMember_139" },
    { _type: "teamMember", _id: "teamMember_14" },
    { _type: "teamMember", _id: "teamMember_142" },
    { _type: "teamMember", _id: "teamMember_145" },
    { _type: "teamMember", _id: "teamMember_146" },
    { _type: "teamMember", _id: "teamMember_147" },
    { _type: "teamMember", _id: "teamMember_148" },
    { _type: "teamMember", _id: "teamMember_149" },
    { _type: "teamMember", _id: "teamMember_150" },
    { _type: "teamMember", _id: "teamMember_151" },
    { _type: "teamMember", _id: "teamMember_154" },
    { _type: "teamMember", _id: "teamMember_155" },
    { _type: "teamMember", _id: "teamMember_156" },
    { _type: "teamMember", _id: "teamMember_157" },
    { _type: "teamMember", _id: "teamMember_158" },
    { _type: "teamMember", _id: "teamMember_160" },
    { _type: "teamMember", _id: "teamMember_19" },
    { _type: "teamMember", _id: "teamMember_23" },
    { _type: "teamMember", _id: "teamMember_29" },
    { _type: "teamMember", _id: "teamMember_3" },
    { _type: "teamMember", _id: "teamMember_31" },
    { _type: "teamMember", _id: "teamMember_34" },
    { _type: "teamMember", _id: "teamMember_4" },
    { _type: "teamMember", _id: "teamMember_46" },
    { _type: "teamMember", _id: "teamMember_50" },
    { _type: "teamMember", _id: "teamMember_55" },
    { _type: "teamMember", _id: "teamMember_58" },
    { _type: "teamMember", _id: "teamMember_59" },
    { _type: "teamMember", _id: "teamMember_6" },
    { _type: "teamMember", _id: "teamMember_61" },
    { _type: "teamMember", _id: "teamMember_65" },
    { _type: "teamMember", _id: "teamMember_66" },
    { _type: "teamMember", _id: "teamMember_7" },
    { _type: "teamMember", _id: "teamMember_70" },
    { _type: "teamMember", _id: "teamMember_76" },
    { _type: "teamMember", _id: "teamMember_77" },
    { _type: "teamMember", _id: "teamMember_78" },
]
Mar 4, 2021, 10:01 PM
If you are able to reproduce it using a simplified script, we'd love to see a GitHub issue on it!
Mar 5, 2021, 2:01 AM
Bah! Created a really simplified version of the same request with Sanity Client and a simplified Node Fetch mutation and they both now work. Must’ve been something with my previous imported data 😕 thanks for your time.
Mar 5, 2021, 1:29 PM
I got this error happening to me. Seemed to be an id conflict as suggested. I didn't realise that the ids are global rather than scoped to a type. the data source i was coming from (3rd party) obviously was doing them as scoped to types
Mar 12, 2021, 12:53 AM

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?