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'
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.
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.For numeric IDs, a form similar to the query-by-name works:
I found this form especially useful in the Admin Console.
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.
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: