Google App Engine/NDB: "BadValueError: Expected Key, got None" for saving perfectly fine entity

Google App Engine/NDB: "BadValueError: Expected Key, got None" for saving perfectly fine entity

我目前正在将 Web 应用程序移植到 Google App Engine,Python 3,标准环境。 (所以我是 GAE 和 NDB 的新手)。

我这里有一个我不明白的错误。我得到一个像这样的实体,调整属性,然后保存。

sourceUsers = User.query().filter(User.email == request.form.get('sourceUserEmail')).fetch(1)
if sourceUsers:
    sourceUser = sourceUsers[0]
    # manipulation:
    sourceUser.sentInvitationSubscriptionKeys.append(subscription.key)
    # verify key:
    assert sourceUser.key != None
    sourceUser.put()

但是保存会触发以下回溯:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/yanone/Code/Google App Engine/typeworldappengine/main.py", line 500, in api
    sourceUser.put()
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/_options.py", line 89, in wrapper
    return wrapped(*pass_args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/utils.py", line 78, in wrapper
    return wrapped(*args, **new_kwargs)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/utils.py", line 109, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4914, in _put
    return self._put_async(_options=kwargs["_options"]).result()
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 190, in result
    self.check_success()
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 137, in check_success
    raise self._exception
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 312, in _advance_tasklet
    yielded = self.generator.send(send_value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4974, in put
    ds_entity = _entity_to_ds_entity(self)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 722, in _entity_to_ds_entity
    names = prop._to_datastore(entity, data)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 2063, in _to_datastore
    value = self._get_base_value_unwrapped_as_list(entity)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1551, in _get_base_value_unwrapped_as_list
    wrapped = self._get_base_value(entity)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1537, in _get_base_value
    return self._apply_to_values(entity, self._opt_call_to_base_type)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1834, in _apply_to_values
    value[:] = map(function, value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1612, in _opt_call_to_base_type
    value = _BaseValue(self._call_to_base_type(value))
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1671, in _call_to_base_type
    return call(value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 1801, in call
    new_value = method(self, value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 3344, in _validate
    "Expected Key, got {!r}".format(value)
google.cloud.ndb.exceptions.BadValueError: Expected Key, got None

在调试中,我什至确保实体有一个键(assert sourceUser.key != None)。所以我真的不明白。我做错了什么?

请注意,所有其他实体都可以保存。只有这个没有。

谢谢。

我不认为这个实体的主键有问题。它可能是对象上的另一个关键字段。

其余用户对象的架构是什么?