Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query in GQL using the entity key

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'
like image 483
Thilo Avatar asked Jan 04 '10 07:01

Thilo


People also ask

What is the query language we can use with Datastore?

The Python Datastore API provides two classes for preparing and executing queries: Query uses method calls to prepare the query. GqlQuery uses a SQL-like query language called GQL to prepare the query from a query string.

What is an entity in Datastore?

Data objects in Firestore in Datastore mode are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind do not need to have the same properties, and an entity's values for a given property do not all need to be of the same data type.


5 Answers

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.

like image 112
Adam Crossland Avatar answered Sep 25 '22 08:09

Adam Crossland


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.

like image 30
Jeffrey Miller Avatar answered Sep 26 '22 08:09

Jeffrey Miller


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.

like image 29
Nick Johnson Avatar answered Sep 26 '22 08:09

Nick Johnson


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.

like image 36
aij Avatar answered Sep 23 '22 08:09

aij


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:

SELECT * FROM mytype WHERE __key__ = KEY('mytype', '12345')

But this does:

SELECT * FROM mytype WHERE __key__ = KEY(mytype, 12345)
like image 34
Tim Consolazio Avatar answered Sep 23 '22 08:09

Tim Consolazio