问题描述
我发现了一个奇怪的问题,即运行查询、确认记录存在、返回计数为零.
I found this peculiar problem where running a Query, confirming the record exists, returns a count of zero.
这是我的模型:
class Description(ndb.Model):
description = ndb.TextProperty()
time_posted = ndb.DateTimeProperty(auto_now_add=True)
uuid = ndb.StringProperty()
class Examine(ndb.Model):
updated = ndb.DateTimeProperty(auto_now=True, auto_now_add=True)
descriptions = ndb.StructuredProperty(Description, repeated=True)
active = ndb.KeyProperty(kind=Description)
slug = ndb.StringProperty(indexed=True)
假设我正在运行以下命令,确认数据存储中确实存在特定的 UUID:
Assume that I'm running the following, confirming that the specific UUID does exist in the datastore:
d_id = 'ef531b70-3486-11e3-9500-ef31d661e6b2'
cnt = Description.query(Description.uuid == d_id).count()
我将收到 0 作为 cnt 的结果.有人可以向我解释为什么会这样吗?
I will receive 0 as a result for cnt. Could somebody explain to me why this is happening?
推荐答案
数据存储查询最终是一致的.这意味着如果基础数据发生更改,有时查询将无法反映此更改.
Datastore queries are eventually consistent. Meaning that if the underlying data changes, sometimes a query will fail to reflect this change.
要解决此问题,您可以将数据存储区和查询构建为高度一致:https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency
To remedy this you can structure your datastore and queries to be strongly consistent: https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency
如果描述实体保存在检查的父键中,那么以下查询将是强一致性的:
If the description entity was saved within a parent key of examine then the following query would be strongly consistent:
cnt = Description.query(ancestor=ExamineKey).filter(Description.uuid == d_id).count()
这篇关于NDB 查询返回零结果.数据存储显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!