Querying the datastore when an ancestor does not exist

180 views
Skip to first unread message

John Beckett

unread,
May 30, 2016, 8:18:15 PM5/30/16
to google-appengine-go
I want to get a child entity where I know what the ID of the parent should be.  However, I am getting this information from an API request, and can't be 100% certain that the client sending the request doesn't make a mistake.  Additionally I don't want to have to try to get the ancestor first to be sure it exists.

So what would the response be if I try to get an entity, but the ancestor key provided in the query doesn't exist?  If I get a datastore.ErrNotExist, is there a simple way of determining whether it is the ancestor that doesn't exist, or the entity being retrieved?

Dave Day

unread,
May 30, 2016, 8:48:58 PM5/30/16
to John Beckett, google-appengine-go
You can make queries using an ancestor key even if that ancestor doesn't itself exist in the datastore.

In fact the integration test for the google.golang.org/cloud/datastore package (same service, just designed for use outside AE) uses this as a way to enforce strong consistency in queries: https://github.com/GoogleCloudPlatform/gcloud-golang/blob/master/datastore/integration_test.go#L364

John Beckett

unread,
May 31, 2016, 7:01:35 AM5/31/16
to Dave Day, google-appengine-go
In my case, I want to be sure that the ancestor exists before adding a child entity to a non-existing ancestor.  Is there any simpler way to check if the ancestor exists other than trying to get it?

Ronoaldo José de Lana Pereira

unread,
May 31, 2016, 1:45:35 PM5/31/16
to John Beckett, Dave Day, google-appengine-go
Think of the ancestor as part of the key of the child entry, and nothing more than that. They exist as two independent items in the datastore, one just has the parent key as some kind of "prefix", and this prefix allow some internal datastore things to happen efficiently, like allowing transactions on this group, and so on.

If you datastore.Get() the child key, and get an ErrDoesNotExist, you have no way to determine in this call if the ancestor exists or no. All you are sure is that this particular key (the child one) is not there.

Here are some things you may try:

1/ Do a GetMulti on both keys, the ancestor and the child entity, so you can in a single call determine if both exist. This should be OK to do in a single txn, since you are touching a single entity group.
2/ Do a keys-only kindless ancestor query. This wil return all keys in the entity group, so you can check if both the parent and a set of child keys are there. This, as beign an ancestor key, should be OK to do inside a txn as well.

Hope that helps!

--
You received this message because you are subscribed to the Google Groups "google-appengine-go" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronoaldo Pereira

John Beckett

unread,
May 31, 2016, 6:26:26 PM5/31/16
to Ronoaldo José de Lana Pereira, Dave Day, google-appengine-go
Thanks Ronoaldo, that clears it up for me.
Reply all
Reply to author
Forward
0 new messages