您好,我正在尝试列出包含给定艺术家的歌曲的播放列表,我有其 key 名称,我的模型是:
**playlist**
songs = db.ListProperty(db.Key)
**Song**
artist = db.ReferenceProperty(Artist,collection_name='songs')
**Artist**
Key Name = john-lennon
因此播放列表包含歌曲键列表,并且歌曲包含对艺术家的引用。
任何帮助将不胜感激,到目前为止我的查询一直都很简单,所以我现在有点迷失了:-)
最佳答案
您实际上无法使用 GAE 执行这样的查询。数据存储不是关系数据库,并且不执行诸如 JOIN 之类的操作。
您能做的最好的事情就是获取艺术家是约翰·列侬的所有歌曲,然后找到这些歌曲的播放列表:
songs = GqlQuery('SELECT __key__ FROM song WHERE artist = :1', 'john-lennon')
playlists = GqlQuery('SELECT * FROM playlist WHERE songs IN :1', songs)
请注意,如果您有超过 30 首 John Lennon 的歌曲,即使这样也会出现问题 - 正如文档所述:
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.
关于python - 帮助进行数据存储查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6010240/