Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Error migrating data with CLI due to non-existent document reference

33 replies
Last updated: Jan 19, 2023
I have a singleton document set up following the guide here: https://www.sanity.io/guides/singleton-document
When I attempt to migrate data from staging to production using the CLI with
--replace
, I get the following error:
  sanity:client [589] Response body: {
  "error": {
    "description": "Mutation(s) failed with 1 error(s)",
    "items": [
      {
        "error": {
          "description": "Document \"landingPage\" references non-existent document \"6e064ed9-c668-4084-a803-8166a5a970f1\"",
          "id": "landingPage",
          "referenceID": "6e064ed9-c668-4084-a803-8166a5a970f1",
          "type": "documentReferenceDoesNotExistError"
        },
        "index": 0
      }
    ],
    "type": "mutationError"
  }
The only reference to this document is in the desk structure per that guide. Is there a way to get around this? I’m also confused because
landingPage
is not any of our types
Jan 19, 2023, 5:53 PM
It looks like the document with
_id: landingPage
is referencing a document that doesn’t exist in the new dataset. What does that landing page document look like?
Jan 19, 2023, 6:28 PM
import { DocumentDefinition } from "sanity"

export const etaLanding2021: DocumentDefinition = {
  name: "etaLanding2021",
  type: "document",
  title: "ETA 2021 Landing",
  preview: {
    prepare: () => ({
      title: "ETA 2021 Landing Page",
    }),
  },
  groups: [
    {
      name: "pageData",
      title: "Page Data",
      default: true,
    },
    {
      name: "seo",
      title: "SEO",
    },
  ],
  fields: [
    { type: "seo", group: "seo", name: "seo" },
    {
      title: "hero banner logo",
      name: "heroImage",
      type: "reference",
      to: [{ type: "reusableImage" }],
      group: "pageData",
    },
    {
      type: "string",
      title: "Youtube video (ID)",
      name: "youtubeVideoId",
      group: "pageData",
    },
    {
      type: "image",
      title: "Youtube video Poster image",
      name: "youtubeVideoPoster",
      group: "pageData",
    },
  ],
}
Jan 19, 2023, 6:29 PM
oooooooo
Jan 19, 2023, 6:29 PM
the reusable image
Jan 19, 2023, 6:29 PM
🤦‍♂️
Jan 19, 2023, 6:29 PM
Nailed it!
Jan 19, 2023, 6:33 PM
hm interesting, moving that over also failed with a mutation error
Jan 19, 2023, 6:33 PM
but that reusable image has no references
Jan 19, 2023, 6:33 PM
Did you also bring the asset?
Jan 19, 2023, 6:34 PM
it seems to re-trigger that same landing page error
Jan 19, 2023, 6:34 PM
when i try to import only the re-usable image types
Jan 19, 2023, 6:34 PM
this does include assets, yes
Jan 19, 2023, 6:34 PM
in all of the imports in question
Jan 19, 2023, 6:34 PM
 [  0%] Strengthening references (4.70s)  sanity:client [588] Response code: 409 Conflict +267ms
  sanity:client [588] Response body: {
  "error": {
    "description": "Mutation(s) failed with 1 error(s)",
    "items": [
      {
        "error": {
          "description": "Document \"landingPage\" references non-existent document \"6e064ed9-c668-4084-a803-8166a5a970f1\"",
          "id": "landingPage",
          "referenceID": "6e064ed9-c668-4084-a803-8166a5a970f1",
          "type": "documentReferenceDoesNotExistError"
        },
        "index": 0
      }
    ],
    "type": "mutationError"
  }
} +0ms
Jan 19, 2023, 6:35 PM
Can you try adding the
--allow-failing-assets
flag to see if the assets may be the issue?
Jan 19, 2023, 6:36 PM
Same error
Jan 19, 2023, 6:38 PM
I also tried re-publishing the doc with that field blank to hopefully “remove” the reference but the vision tool confirms it is still there
Jan 19, 2023, 6:38 PM
oh hmm. it seems like the singleton setup isn’t actually showing me the broken document.. it allowed me to create a new one
Jan 19, 2023, 6:39 PM
ok, well this is going to take some cleanup
Jan 19, 2023, 6:40 PM
ok, all set
Jan 19, 2023, 6:58 PM
Nice!
Jan 19, 2023, 7:10 PM
ok.. so not quite all set here. No matter what, this error seems to pop up
Jan 19, 2023, 7:23 PM
I have deleted the singleton “landingPage” document from production and am just trying to migrate over the reusable images first.. but I always get that same error when moving over the reusable images
Jan 19, 2023, 7:24 PM
even there are no landing page docs at all
Jan 19, 2023, 7:24 PM
it seems to get pulled in to the import with the reusable images for some reason?
Jan 19, 2023, 7:24 PM
and the reusable images don’t actually import
Jan 19, 2023, 7:25 PM
for some reason, when the assets get imported that document is created no matter what.. I think maybe because there is an asset (image) field in the document?
Jan 19, 2023, 7:29 PM
I confirmed the NDJson does not have the data for that document in it.. so somehow the data gets there via the assets json
Jan 19, 2023, 7:30 PM
in the studio I get a warning about strong vs weak references:
Jan 19, 2023, 7:33 PM
if I explicitly set weak to true in the schema it goes away. I did not have this specified before
Jan 19, 2023, 7:35 PM
ok well, I have no idea how I pulled it off but I got all the data over there. I think the key was throwing in a
--raw
export at some point along the way - this got the reusable image documents there but none of the assets associated (the image field in all docs was blank)
Jan 19, 2023, 7:45 PM
I also then had to change that field above to be strong, again, in the production dataset
Jan 19, 2023, 7:46 PM
I think that weak reference may have been happening because the import was failing while strengthening references.
Jan 19, 2023, 7:48 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?