help with datastore query
Hi I am trying to list playlists wich contain songs by a given arti开发者_如何学编程st for which i have the keyname, my models are:
**playlist**
songs = db.ListProperty(db.Key)
**Song**
artist = db.ReferenceProperty(Artist,collection_name='songs')
**Artist**
Key Name = john-lennon
so playlist contains a list of song keys, and a song contains a reference to the artist.
any help would be appreciated, My queries have always been simple so far so im a little lost now :-)
You can't really do queries like this with GAE. The datastore is not a relational database, and does not do things like JOINs.
The best you can do is to get all the songs whose artist is John Lennon, and then find the playlist for those songs:
songs = GqlQuery('SELECT __key__ FROM song WHERE artist = :1', 'john-lennon')
playlists = GqlQuery('SELECT * FROM playlist WHERE songs IN :1', songs)
Note that even this will have problems if you have more than 30 songs by John Lennon - as the documentation says:
Note: The IN and != operators use multiple queries behind the scenes. For example, the IN operator executes a separate underlying datastore query for every item in the list. The entities returned are a result of the cross-product of all the underlying datastore queries and are de-duplicated. A maximum of 30 datastore queries are allowed for any single GQL query.
精彩评论