NDB query returns zero results. Datastore shows the result

醉酒当歌 提交于 2019-12-12 15:24:09

问题


I found this peculiar problem where running a Query, confirming the record exists, returns a count of zero.

Here are my models:

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)

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()

I will receive 0 as a result for cnt. Could somebody explain to me why this is happening?


回答1:


Datastore queries are eventually consistent. Meaning that if the underlying data changes, sometimes a query will fail to reflect this change.

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()


来源:https://stackoverflow.com/questions/19353509/ndb-query-returns-zero-results-datastore-shows-the-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!