MeLight MeLight - 3 months ago 20
Python Question

Getting "Expected string, got None" error on Model.get_by_id()

When running this code, with the same site_id:

int_id= int(self.request.get("site_id"))
site_draft = SiteDraft.get_by_id(int_id)


I'm getting this error:

INFO 2016-06-27 12:39:19,040 module.py:788] minisites: "GET /edit/5891733057437696 HTTP/1.1" 500 -
return self.handle_exception(e, self.app.debug)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\Users\Yuri\Documents\WiseStamp\Server\minisites\web\pages\edit_site.py", line 21, in get
self.post(args)
File "C:\Users\Yuri\Documents\WiseStamp\Server\minisites\web\pages\edit_site.py", line 44, in post
site_draft = SiteDraft.get_by_id(int_id)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\utils.py", line 160, in positional_wrapper
return wrapped(*args, **kwds)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 3602, in _get_by_id
return cls._get_by_id_async(id, parent=parent, **ctx_options).get_result()
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 378, in get_result
self.check_success()
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 425, in _help_tasklet_along
value = gen.send(val)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\context.py", line 765, in get
pbs = entity._to_pb(set_key=False).SerializePartialToString()
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 3167, in _to_pb
prop._serialize(self, pb, projection=self._projection)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1422, in _serialize
values = self._get_base_value_unwrapped_as_list(entity)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1192, in _get_base_value_unwrapped_as_list
wrapped = self._get_base_value(entity)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1180, in _get_base_value
return self._apply_to_values(entity, self._opt_call_to_base_type)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1352, in _apply_to_values
value[:] = map(function, value)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1234, in _opt_call_to_base_type
value = _BaseValue(self._call_to_base_type(value))
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1255, in _call_to_base_type
return call(value)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1331, in call
newvalue = method(self, value)
File "C:\Program Files\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 1781, in _validate
(value,))
BadValueError: Expected string, got None


It used to work a day ago, but now it works only 20% of the time (which is also weird - it seems to be working some times). What's even weirder that I'm providing an
int
as the
site_id
, but it tells me it gets a
None
.

Please advise.

Answer

As request

Looking at the stack trace in detail and paying attention to the operations performed,

we see

 _get_by_id
 get_result
 _help_tasklet_al
 get
 _to_pb
 _serialize
 _get_base_value
_get_base_value
_apply_to_values
_opt_call_to_base
_call_to_base_type
call
_validate


BadValueError: Expected string, got None

We see that the path of execution was inside _get_by_id and it was processing get_result. So an entity has been retrieved. Looking further down we see that the code is in fact validating the values retieved and fails in the _validate call with the BadValueError: Expected string, got None

How does one get a bad value in an existing entity. Typically it is as a result of changing the model where you either

  • added a new required property and not updating existing entities.,
  • changed the type of an existing property
  • added a validator to a property and not made existing entities compliant.

When you change Models it is important to consider entity migration.

Cheers

T