如何使用实体键在 GQL 中查询
如何在 Google App Engine 数据查看器中使用 GQL 编写针对实体键的查询?
在查看器中,第一列 (Id/Name) 显示为 name=_1
,在详细信息视图中,它显示的键为
Decoded entity key: Programme: name=_1
Entity key: agtzcG9...................
This query does not work:
SELECT * FROM Programme where name = '_1'
How do I write a query against the entity key using GQL in the Google App Engine Data Viewer ?
In the viewer, the first column (Id/Name) displays as name=_1
, in the detail view it shows the key as
Decoded entity key: Programme: name=_1
Entity key: agtzcG9...................
This query does not work:
SELECT * FROM Programme where name = '_1'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用实体的密钥来检索它:
并且,您应该能够使用类似的名称进行查询:
请注意,这不是您希望在 AppEngine 应用程序中执行的操作;正如尼克在评论中指出的那样,这是对时间的巨大浪费。实际上,这个例子只是为了向您展示如何在管理控制台中通过 Key 进行查询。
You can use the entity's key to retrieve it:
And, you should be able to query using the name similarly:
Note that this is not something that you would want to do in your AppEngine application; as Nick notes in his comment, it is a huge waste of time. Really, this example is only good to show you how to query by Key in the Admin console.
对于数字 ID,可以使用类似于按名称查询的表单:
我发现此表单在管理控制台中特别有用。
For numeric IDs, a form similar to the query-by-name works:
I found this form especially useful in the Admin Console.
您根本不需要查询来通过键获取实体 - 您可以简单地通过键获取实体。在 Python 中,您可以使用
MyModel.get_by_key_name('_1')
来执行此操作。这比 Adam 建议的使用查询快 3 到 5 倍。You don't need to query to get an entity by key at all - you can simply fetch the entity by its key. In Python, you can do this with
MyModel.get_by_key_name('_1')
. This is 3 to 5 times faster than Adam's suggestion of using a query.按键查询时,需要精确匹配键,包括父项,而不仅仅是 ID 或名称。当然,如果parent为null,如上例所示,实体的ID或Name和类型就足够了。
如果您有已经编码的实体密钥,则可以使用它,例如:
对于上面的简单示例,
就可以了,但是如果您的密钥有一个父项,例如
那么查询将是
如果父项本身有一个父项,则需要也添加一下。如需了解更多详情,请参阅官方 GQL 文档。
似乎没有一种方法可以选择具有相同 ID 或名称的所有内容,而不管其父级如何。
When querying by key, you need to match the key exactly, including the parent and not just the ID or name. Of course, if the parent is null, as in the example above, the ID or Name and the type of entity is enough.
If you have the already encoded entity key, you can just use that like:
For the simple example above,
will do, but if your key has a parent, like
Then the query would be
If the parent itself has a parent, you need to add that too. For more details see the official GQL documentation.
There does not appear to be a way to select everything with the same ID or name regardless of parent.
对此简单说明一下:当我在 KEY 中的任何参数周围使用任何引号时,调用会失败(在管理控制台中,我收到错误弹出窗口)。
例如,对于 ID/名称为 12345 的“mytype”类型,这不起作用:
但这确实有效:
Just a quick note on this: When I use any quotes around any of the args in KEY, the call fails (in the admin console I get the error popup).
For example, for type "mytype" with ID/Name 12345, this does NOT work:
But this does: