I've read a lot about how I need to rethink implementing entities and joins when developing for AppEngine, and I still can't figure out how to do this:
I've 3 entities. Tag has many Tweets and User Follow many Tags.
I would like to get all Tweets that is Tagged with tags that a User follows.
I thought eliminating Tags entity and using StringListProperty might work as thi开发者_开发百科s:
Tweets.all().filter(‘tag IN”, user.tags)
However, as in the ListProperty documentations indicated, this won’t work! as because a query can’t compare two lists.
A query cannot compare two list values. There is no way to test two lists for equality without testing each element for membership separately.
Any idea how to structure the entities in order I can be able to answer the question:
How to get all Tweets that is Tagged with tags that a User follows?
Thanks
You're not trying to compare two lists - you're checking for intersection. The query you list will do that, with one major caveat: IN filters are split up into multiple sub-queries, which are executed independently by the backend, with a maximum of 30 sub-queries.
That is, if your user follows 3 tags ['foo', 'bar', 'baz'], doing the above is equivalent the union of:
Tags.all().filter('tag =', 'foo')
Tags.all().filter('tag =', 'bar')
Tags.all().filter('tag =', 'baz')
You should find this article on managing entity relationships helpful.
Since each Tweet can have more than one tag, you'll need to follow the "Many to Many" section of this article. You'll probably want do the same thing for relationships between Users and Tags. This will then give you an efficient way to get all Tweets corresponding to Tags followed by a User.
精彩评论