Search code examples
google-app-enginedatastoreapp-engine-ndb

NDB Query not returning full object


I am doing a NDB query, which seems to only be fetching a partial object. For the Model, I've turned off caching, in case that was it. However, a number of properties are coming back with None, when I can see them filled in the Datastore Viewer.

This is with the local development server ( and deployed), and the query is being done by a Backend process.

Note: Clearing the memcache did not help.

NOTE: If I cause the backend to restart, it will start pulling down the correct data.

Basically:

Backend starts querying for instances of a Model every X seconds

Frontend causes a change to an instance of the Model

Backend continues to see the original version of the instance until restarted

Backend code is pretty simple:

while 1:
    time.sleep(2)
    q = None
    res = None
    q = core.Agent.query()
    res = q.fetch(10)
    for a in res:
        logging.error("%s" % a.to_dict())

Frontend changes some properties (and it shows in the viewer) but the backend will only show old values. It also seems like a Filter will filter based on correct values, but fetch() returns old stuff.


Solution

  • You need to clear the context cache at the top of the loop, e.g.

    while 1:
      ndb.get_context().clear_cache()
      <rest of your code>