Matt
Matt

Reputation: 515

NDB query returns zero results. Datastore shows the result

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?

Upvotes: 3

Views: 921

Answers (1)

Jesse
Jesse

Reputation: 8393

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

Upvotes: 1

Related Questions