Google-cloud-python: Storing time in datastore

Created on 13 Jun 2017  ยท  5Comments  ยท  Source: googleapis/google-cloud-python

I'm trying to store a datetime.time object in datastore but am getting the following error: ValueError: Unknown protobuf attr type <class 'datetime.time'>.

I've reproduced the error:

(venv) โฏ python3
Python 3.6.1 (default, Mar 23 2017, 16:49:06)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import datastore
>>> from datetime import datetime, time
>>> t = time(10, 11, 12)
>>> t.isoformat()
'10:11:12'
>>> client = datastore.Client(<projectid>) # Project ID defined elsewhere
>>> key = client.key("Kind")
>>> entity = datastore.Entity(key=key)
>>> entity.update({"time": t})
>>> client.put(entity)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/client.py", line 384, in put
    self.put_multi(entities=[entity])
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/client.py", line 408, in put_multi
    current.put(entity)
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/batch.py", line 199, in put
    _assign_entity_to_pb(entity_pb, entity)
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/batch.py", line 319, in _assign_entity_to_pb
    bare_entity_pb = helpers.entity_to_protobuf(entity)
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 219, in entity_to_protobuf
    _set_protobuf_value(value_pb, value)
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 408, in _set_protobuf_value
    attr, val = _pb_attr_value(val)
  File "/Users/davidcorbin/Documents/hw-api/venv/lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 325, in _pb_attr_value
    raise ValueError("Unknown protobuf attr type %s" % type(val))
ValueError: Unknown protobuf attr type <class 'datetime.time'>
>>>

Here is my google-cloud version

(venv) โฏ pip list | grep '^google-cloud '
google-cloud                                 0.24.0

What am I doing wrong?/How should I store a time object (e.g. 10:30 AM) in datastore?

bug datastore

All 5 comments

Upgrade to version 0.25.0 and still getting the same error.

@davidcorbin Thanks for the report. Based on the back-end docs for entity property types, there is no "native" way to store time values (as opposed to timestamps, which map both date and time).

Your app would need to store the value using a supported type (e.g., assign entity['time'] = t.isoformat()) and then convert it back to a time instance as needed.

@jonparrott @lukesneeringer @tseaver Do you think we should type-check on __setitem__ / update? That somewhat defeats the purpose of having Entity subclass dict, but @davidcorbin probably would've been happier if he got a TypeError when trying to update the Entity locally rather than when trying to call put.

Instead of trying to nanny-on-insert, I'd rather we just document the allowed types, with explanations on how to accommodate others.

Was this page helpful?
0 / 5 - 0 ratings