How do I query in GQL using the entity key
Solution 1:
You can use the entity's key to retrieve it:
SELECT * FROM Programme where __key__ = KEY('agtzcG9...................')
And, you should be able to query using the name similarly:
SELECT * FROM Programme where __key__ = KEY(Programme, '_1')
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.
Solution 2:
For numeric IDs, a form similar to the query-by-name works:
SELECT * from Programme where __key__ = KEY('Programme', 1234567)
I found this form especially useful in the Admin Console.
Solution 3:
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.
Solution 4:
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:
SELECT * FROM Programme where __key__ = KEY('agtzcG9...................')
For the simple example above,
SELECT * FROM Programme where __key__ = KEY('Programme', '_1')
will do, but if your key has a parent, like
Paren: id=123
Then the query would be
SELECT * FROM Programme where __key__ = KEY('Paren', 123, 'Programme', '_1')
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.