Reputation: 9617
I would like to use an user entered string as the unique Key for an Entity. Suppose an user enters a key that is already in the datastore, I would like to return an error. Since the golang datastore API only has Put which also doubles as Insert, what is the best way to enforce uniqueness constraint?
Currently I'm trying to do…
Query(T).Filter("Key =", key)
…where key is constructed from the user entered value to test the existence of a duplicate, but 2 identical keys seem to return false
with the equality (=
) operator in the filter but true
when calling Equal
on the result.
How do I query by Key?
Upvotes: 2
Views: 262
Reputation: 857
A query by key is called a "get": https://developers.google.com/appengine/docs/go/datastore/entities#Go_Retrieving_an_entity
There's almost no point in doing a query and filtering by key equality.
Upvotes: 1
Reputation: 24808
You should use the magic constant __key__
to filter by keys:
Query(T).Filter("__key__ =", key)
References:
I agree that this is somewhat obscure and under-documented.
Upvotes: 1