Potential reasons for receiving duplicate records in a query and how to debug the issue.

6 replies
Last updated: May 7, 2021
For what reason could I be potentially receiving duplicate records in my query like so
0:Object
_createdAt:"2021-04-21T16:35:41Z"
_type:"page"
_updatedAt:"2021-05-07T20:10:51Z"
pageId:"4b4a7270-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
routeLabel:"testing dynamic"
slug:"sub/directory"
1:Object
_createdAt:"2021-04-21T16:35:41Z"
_type:"page"
_updatedAt:"2021-05-07T20:10:51Z"
pageId:"4b4a7270--xxxx-xxxx-xxxx-xxxxxxxxxxxx"
routeLabel:"testing dynamic"
slug:"sub/directory"
If my query looks like this.

*[_type == "route" && slug_custom_format.current != "index"]{
    routeLabel,
    "slug": slug_custom_format.current,
    ...page->{
      _createdAt,
      "pageId": _id,
      _type,
      _updatedAt,
    } 
  } | order(_updatedAt desc)
A bit stumped as to why this is happening. There are no drafts of the document.
May 7, 2021, 8:21 PM
*[_type == "route" && slug_custom_format.current != "index" && !(_id in path('drafts.**'))] { ... }
If that still returns two results, then you must have two routes that are matching and reference the same page. For debugging, it’s probably best that you return the
_id
of the route.
May 7, 2021, 8:34 PM
*[_type == "route" && slug_custom_format.current != "index" && !(_id in path('drafts.**'))] { ... }
If that still returns two results, then you must have two routes that are matching and must reference the same page. For debugging, it’s probably best that you return the
_id
of the route.
May 7, 2021, 8:34 PM
Ahh! Thanks for the perspective Geoff,I hid that route _id because I didn't need it, and turns out there is a draft _id there.
This leads me to some interesting questions. Mostly about the structuring I am using.

For context I am messing around with a previewing in Sanity with a non-react front-end. So I do in fact want access to the draft documents.

I think I am running into a circular references issue here though as my larger issue.
Ran into some weird things when trying to preview
1. 1-2 ->This is where the draft is, but I don't remember changing it so attempt to revert to published
3. Says it's successful restoring the document
4. Publish and get this message.
5. Culprit
May 7, 2021, 10:40 PM
Great! Glad you found that.
There were a few days when
v2.8.0
was the latest version where drafts could be referenced. It was quickly fixed , but unfortunately any references created during that time that pointed to a draft would need to be corrected manually. Based on the error you’re getting, I suspect that’s what’s happening.
May 7, 2021, 10:56 PM
Yep that's definitely what it was. Fixing it manually seemed to do the trick.Just a matter of some quick clicks and publishes to get it back.
Thanks again for the insight.
May 7, 2021, 11:50 PM
Yep that's definitely what it was. Fixing it manually seemed to do the trick.Just a matter of some quick clicks and publishes to get it back.
Thanks again for the insight.
May 7, 2021, 11:50 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?