Question

I have

class Question(db.Model):
    wording = db.StringProperty(required=True)

class Answer(db.Model):
    question = db.ReferenceProperty(Question, required=True)
    userId = db.IntegerProperty(required=True)

And I want to

SELECT * FROM Question WHERE Id NOT IN (
       SELECT QuestionId FROM Answer WHERE userId = 1)

How to do that in GQL

Was it helpful?

Solution

GQL doesn't have a NOT IN statement, so unfortunately, it's not possible to do exactly what you want.

If the total set of userIds is small, you can reverse your query to use the IN statement. For instance:

SELECT * FROM Answer WHERE userId IN ('2', '3')

Note that this is executing a subquery for each value in the IN statement and you are allowed a maximum of 30 queries per GQL statement.

If the total set of Answers where userId = 1 is small, you can select all Answers and filter out the ones from userId = 1 in your own code. However, if the set of Answers from userId 1 is large, this will not be particularly efficient.

Finally, rather than computing either of these queries on-demand, you can use a cron to precompute the results (and store them to the datastore and/or memcache). When your code requires the results of one of these queries, you can load the cached result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top