GQL query with numeric id in datastore viewer

2019-03-07 22:03发布

I want to build GQL query to get an object using its numeric id. I'm doing this in Datastore viewer in App management console, so I can't use Model.get_by_id(numeric_id). Something like

SELECT * FROM Model WHERE id = <numeric_id>

also doesn't work.

4条回答
混吃等死
2楼-- · 2019-03-07 22:34

In my case I had to change the type of ID from String to Long

查看更多
戒情不戒烟
3楼-- · 2019-03-07 22:40

Try this:

SELECT * FROM Model where __key__ = KEY('Model', <numeric_id>)
查看更多
成全新的幸福
4楼-- · 2019-03-07 22:50

Unfortunately, there does not appear to be a way to write a query equivalent to

SELECT * FROM Model WHERE id = <numeric_id>

which would select all Model entities with the given id. If you're ok with something equivalent to

SELECT * FROM Model WHERE id = <numeric_id> AND parent IS NULL

you can use something like

SELECT * FROM Model where __key__ = KEY('Model', <numeric_id>)

If your entity does have a parent though, you'll need to specify that as part of the key, like

SELECT * FROM Model where __key__ = KEY('ParentModel', <parent_name_or_id>, 'Model', <numeric_id>)

If the parent itself has a parent, you'll need to specify that too. (Grandparent goes left of the parent, and so on.)

Of course if you're not restricted to GQL (like if you're using Python, Go, or Java), you can query the keys, decode them and filter by id, then fetch the corresponding entities. But of course that doesn't work in the Datastore Viewer since you can only use GQL.

查看更多
做自己的国王
5楼-- · 2019-03-07 22:58

Another way around is, first get the key for the entity using the id by

key = db.Key.from_path('Model', int(id))

then get the object by

obj = db.get(key)

The advantage is, you do not have to do any string formatting.

reference: problem set 3 at this course, https://classroom.udacity.com/courses/cs253/

查看更多
登录 后发表回答