Reputation: 13376
I have the following models, where one Thing
can have multiple Action
:
class Thing(polymodel.PolyModel):
create_date = db.DateTimeProperty(required=True)
class Action(db.Model):
thing = db.ReferenceProperty(Thing, collection_name='action')
action_by = db.StringProperty(required=True)
I need to use raw GQL to count the number of Thing
s that have no actions. It should be something like:
SELECT * FROM Thing WHERE action = []
I have the following limitations:
Can it be done?
Upvotes: 0
Views: 104
Reputation: 599490
Firstly, you're completely mistaken to say that you have to use GQL because normal queries don't support DISTINCT: there is nothing you can do with GQL that you can't do with a normal query. The datastore is not a database, and does not have an underlying query language that ORM calls must be translated to; on the contrary, in fact GQL calls are translated into RPC calls in exactly the same way as model calls, and there is no benefit to using GQL at all. In this specific case, the Query
class has a distinct
parameter.
However, another implication of the datastore not being an SQL database is that you cannot do JOINs. There is no way to select instances of Thing based on any property in Action, whether it's a specific field value or the absence of any relation. The only way to do this would be to get all distinct values of Action.thing
, then all Things
, and work out the set difference.
Upvotes: 3