【问题标题】:Fetching entities from datastore where Entity.key.IN([keys...])从 Entity.key.IN([keys...]) 的数据存储中获取实体
【发布时间】:2015-05-30 00:54:32
【问题描述】:

我正在尝试获取一长串实体,这些实体都引用了几个不同的相关实体之一。 cmets 中对此进行了解释,但基本上许多“项目”都引用了一些“公司”。我不想对unique_key(即key.get())中的每个key 进行多次查询,所以我认为以下方法可行,但它返回一个空列表。祈祷告诉我,我做错了什么?或者有没有更好的方法来完成许多项目引用一些项目的这种关系,同时最大限度地减少对数据库的调用(我是 AppEngine Datastore 的新手)。

注意,这是用 Python 编写的,使用的是应用引擎提供的 ndb 库。

# "items" is a list of entities that have a property "parenty_company"
# parent_company is a string of the Company key
# I get a unique list of all Key strings and convert them to Keys
# I then query for where the Company Key is in my unique list

unique_keys = list(set([ndb.Key(Company, prop.parent_company) for prop in items]))
companies = Company.query(Company.key.IN(unique_keys)).fetch()

【问题讨论】:

  • 为什么不使用ndb.get_multi(unique_keys)

标签: database google-app-engine google-cloud-datastore app-engine-ndb


【解决方案1】:

您绝对应该使用ndb.get_multi(unique_keys)。它将在一个批次中异步获取所有密钥。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多