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
GQL doesn’t have a
NOT INstatement, 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
INstatement. For instance:Note that this is executing a subquery for each value in the
INstatement and you are allowed a maximum of 30 queries per GQL statement.If the total set of Answers where
userId = 1is small, you can select all Answers and filter out the ones fromuserId = 1in 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.