0

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?

Tzach
  • 12,889
  • 11
  • 68
  • 115
  • What kind of data volume are you planning on? I.e., what's your expectation on the number of Thing and Event entities? How often will you need to do this query, and how timely do you require the answer to be? – Dave W. Smith Oct 21 '13 at 01:10
  • Thanks for the response. There should be tens of thousands of Thing and about the same amount of Action. The queries should run about a few hundred times a day and the main concern should be not downloading all data but just counting it. I know I can implement this in python on the GAE server, but I was looking for a simpler solution, if exists. – Tzach Oct 21 '13 at 05:40

1 Answers1

3

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • In addition you can not query for something with no value set, which is different to something being set to None. In the code above if no key/object was supplied for the reference property, then there is no value you can can query for. – Tim Hoffman Oct 20 '13 at 13:46