python - 帮助进行数据存储查询

标签 python google-app-engine google-cloud-datastore gql

您好,我正在尝试列出包含给定艺术家的歌曲的播放列表,我有其 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/

相关文章:

python - 使用 Python beautifulSoup 抓取特定的 dd 项目

python - 如何提高 Google App Engine 上 request_logs 的吞吐量

php - GAE、PHP 和 GCM : failed to open stream: HTTP request failed! HTTP/1.0 405 方法不允许

google-app-engine - db.get() 与 db.get_by_key_name() 性能对比(GAE BIgtable/Datastore)

python - 对于应用程序的用户,以 "easy"的方式将数据导入 Google App Engine

java - 如何在将 blob 上传到 Blobstore 的 POST 请求中提交文本字段,并在 blob 的上传处理程序中检索它?

python - 如何使用 .count 计算一个列表中的每个项目出现在 python 中另一个列表中的次数?

python - 当条件为两个时有效地更新一个 numpy 数组

google-app-engine - 如何防止开发数据存储在重启时自行清除

python - SQL炼金术 : eager loading relationships of relationships of relationship