Search code examples
pythongoogle-cloud-platformgoogle-app-engine

'Key' object has no attribute 'database' Error in Google Cloud Ndb


I am running an google appengine app using python 3.9. I use cloud ndb as a database for my app. My app has a customer ndb model. When creating an entity everything works fine. However when I want to update an entity the app throws this error.

Here is my code for adding a new customer

newCustomer = customer( number = number,
               addedBy = ndb.Key('employee',int(employeeId)),
               firstName = firstname,
               lastName = lastname,
               phone = phone,
               countryCode= countryCode)
order_key = newCustomer.put()

Here is my code for updating

customer = ndb.Key('customer',customer_id).get()
customer.firstName = firstName
customer.lastName = lastName
customer.phone = phone
customer.countryCode = countryCode
customer_key = customer.put()

Here is the Error I got

AttributeError at /customer/edit/6199943289110528
'Key' object has no attribute 'database'
Request Method: POST
Request URL:    http://localhost:8000/customer/edit/6199943289110528
Django Version: 4.1
Exception Type: AttributeError
Exception Value:    
'Key' object has no attribute 'database

Solution

  • I think it has something to do with the version of the google-cloud-ndb. I reinstall the latest version of google-cloud-ndb with pip install -r requirements.txt and the error goes away.