使用golang在Appengine数据存储区中强制键唯一性的最佳方法是什么(在性能方面)?

使用golang在Appengine数据存储区中强制键唯一性的最佳方法是什么(在性能方面)?

问题描述:

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?

我想使用用户输入的字符串作为实体的唯一键。 假设用户输入了数据存储区中已经存在的密钥,我想返回一个错误。 由于golang数据存储区API仅具有Put项,而Put项也可以作为Insert项,因此实施唯一性约束的最佳方法是什么? n

  Query(T).Filter(“ Key =”,key)
  code>  pre> 
 
 

…其中,键由用户输入的值构造而成,以进行测试 存在重复项,但是2个相同的键似乎在过滤器中返回等于( = code>)运算符的 false code>,但在调用等于 code>。 p>

如何按键查询? p> div>

You should use the magic constant __key__ to filter by keys:

Query(T).Filter("__key__ =", key)

References:

Java API constant values

Python documentation

I agree that this is somewhat obscure and under-documented.

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.