【发布时间】:2016-02-04 14:03:41
【问题描述】:
当我将消息插入数据存储区时,我使用消息的序列号创建一个键,并与发送消息的用户创建祖先关系。当我尝试使用仅从序列号创建的密钥检索消息时,它失败了。如果我将插入更改为使用仅基于序列号的键,则稍后的检索会成功。
代码方面
失败:
存储:
p_key = ndb.Key(StoredBcastMsg,sendingUser)
c_key = ndb.Key(StoredBcastMsg,prof['seqNum'],parent=p_key)
prof['key']=c_key
StoredBcastMsg(**prof).put()
检索失败
msgToRet=ndb.Key(StoredBcastMsg,seqNum).get() #Fails even though sequence number is there in the store
成功了:
存储:
prof['key']=c_key
StoredBcastMsg(**prof).put()
c_key = ndb.Key(StoredBcastMsg,prof['seqNum'])
检索成功:
msgToRet=ndb.Key(StoredBcastMsg,seqNum).get() #Succeeds
这是预期的行为吗?我认为在创建密钥时添加 parent= 标签的唯一区别是创建祖先关系,以便有效地回答诸如“给我用户 X 发送的所有消息”之类的查询。
【问题讨论】:
-
不,您对祖先目的的评估是错误的。请参阅下面的答案。这实际上是关于实体组。如果您之后的所有内容都是父/子关系并且不需要或不想要实体组,则可以使用普通查询。实体组的祖先也不能更改。如果您希望更改实体组中的父级,则必须使用不同的键进行删除和创建
标签: google-app-engine google-cloud-datastore google-app-engine-python ancestor