How to query GQL for boolean value?

1.7k Views Asked by At

There doesn't seem to be any documentation on how to query the datastore using boolean parameters in the where statement of a GQL query.

I have been trying to query an AppEngine datastore based on a boolean column, but it's not returning any results in the data viewer of the AppEngine dashboard.

I've tried the following:

SELECT * FROM users WHERE active=TRUE
SELECT * FROM users WHERE active=True

Any help would be greatly appreciated.

4

There are 4 best solutions below

2
On

As per the documentation the correct syntax for GQL boolean comparisons is

SELECT * FROM users WHERE active = TRUE

See https://developers.google.com/appengine/docs/python/datastore/gqlreference

Now if you are not getting results then either the active attribute is not True for all entities or the attribute active is not in fact a bool type.

Just for completeness I performed the following query with expected results.

SELECT * FROM Plant where flowering = TRUE
0
On

I had to specify True / False (starting with uppercase T / F) for it to work in the query. However, it shows the boolean values in all lowercase in the datastore viewer.

0
On

you can use

    SELECT * FROM users WHERE active = true

make sure that true/false are in small case

this will definately work.

0
On

Please check whether you have an index on the filed you are filtering (in your case make sure that you have indexed "active" property in "users" entity)

I tried this experiment at my end and have seen that if there is no index, datastore doesn't return any results. This is explained in the documentation by Google: Datastore Indexes and the relevant paragraph is:

Every Datastore query computes its results using one or more indexes, tables containing entities in a sequence specified by the index's properties and, optionally, the entity's ancestors...