question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storing time in datastore

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dhermescommented, Jun 13, 2017

@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.

1reaction
tseavercommented, Jun 13, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices | Cloud Datastore Documentation
You can use sharding if you need to write to a portion of the key range at a higher rate than Firestore in...
Read more >
How can I store the date with datastore? - Stack Overflow
You could try storing just the date and put random hours, minutes, and seconds into the timestamp, ...
Read more >
Storing time in datastore · Issue #3497 - GitHub
I'm trying to store a datetime.time object in datastore but am getting the following error: ValueError: Unknown protobuf attr type .
Read more >
The Best Way to Store, Collect and Analyze Time Series Data
Time series data is best stored in a time series database (TSDB) built specifically for handling metrics and events that are time-stamped.
Read more >
Storing UTC date and time in the database - Optimizely
This process should take about a minute for a small site on a fast database server, and up to 30-60 minutes on a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found