从 Entity.key.IN([keys...]) 的数据存储中获取实体

Fetching entities from datastore where Entity.key.IN([keys...])

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

注意,这是在 Python 中,使用 App Engine 提供的 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)。它将在一个批次中异步获取所有密钥。