Tzach
Tzach

Reputation: 13376

GQL query ReferenceProperty for objects with no references

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 Things that have no actions. It should be something like:

SELECT * FROM Thing WHERE action = []

I have the following limitations:

  1. I must use raw GQL (because the actual query contains DISTINCT which is not supported on a regular Query).
  2. I can't fetch the data and check the contents because I use remote api and would like only to count the data to save bandwith.

Can it be done?

Upvotes: 0

Views: 104

Answers (1)

Daniel Roseman
Daniel Roseman

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

Related Questions