Search code examples
pythongoogle-cloud-platformgoogle-cloud-datastore

ndb cloud datastore writes to default namespace


When trying to upgrade from python2 to python3, there was an issue with writing data to different namespaces.

The code was writing to correct namespaces until 14 May, 2020. After that, when trying to write to a specific namespace, it writes to default namespace. How can this be solved?

from google.cloud import ndb
client = ndb.Client()

class ActiveTokens(ndb.Model):
    _memcache_timeout = 60
    namespace = user_namespace
    username = ndb.StringProperty(required=True, indexed=True)
    token = ndb.StringProperty(required=True, indexed=True)
    expiry = ndb.IntegerProperty(required=True)
    otp = ndb.IntegerProperty(indexed=True)
    created = ndb.DateTimeProperty(auto_now_add=True, required=True, indexed=True)
    edited = ndb.DateTimeProperty(auto_now=True, required=True)

    @classmethod
    def store(cls, username, token, expiry, otp):
        with client.context():
            obj = ActiveTokens(username=username,
                                    token=token,
                                    expiry=expiry,
                                    otp=otp,
                                    namespace='user'
                                    )
            return obj.put().id()

Solution

  • I started encountering this issue this morning when deploying my instance to the cloud. It was not occurring on my development environment. My entities were saving under [default] namespace despite setting it in the model. I resolved this issue by downgrading google-cloud-ndb from version "1.2.1" to version "1.1.2".