[PonyORM-list] offset aware datetimes => bad idea with pony orm

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Wed Aug 27 22:43:26 UTC 2014


Thanks for the reporting, I will investigate what is the best way of
dealing with tz-aware datetimes.

One possible solution is this: when tz-aware datetime is assigned as an
attribute value of entity instance, it is immediately converted to naive
UTC datetime. On attribute read its value is converted to tz-aware local
datetime. I will think about implementing such functionality.

PostgreSQL has special datatype for datetimes with timezone, maybe we
should add support for this.

Regards,
Alexander Kozlovsky


On Wed, Aug 27, 2014 at 12:02 AM, stephane martin <stef.martin at gmail.com>
wrote:

> hello,
>
> just a short note: ponyorm doesnt feel very comfortable with tz-aware
> python datetimes in schemas. That's... well... not a surprise, i shoud
> have expected it if i had thought about it 5 minutes.
>
> I have something like:
>
> """
> class Message(db.Entity):
>     id = PrimaryKey(int, auto=True)
>     subject = Optional(unicode)
>     client_hostname = Optional(unicode)
>     helo_hostname = Optional(unicode)
>     message_id = Optional(unicode)
>     importance = Optional(unicode)
>     priority = Optional(unicode)
>     sensitivity = Optional(unicode)
>     size = Optional(int)
>     content_type = Optional(unicode)
>     creator_date = Optional(datetime.datetime)
> """
>
> I feed it with a tz-aware creator_date. Creation succeeds, but
> afterwards updating the object fails with:
>
> """
>   File "<auto generated wrapper of __new__() function>", line 2, in __new__
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/utils.py",
> line 64, in cut_traceback
>     return func(*args, **kwargs)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 3468, in __new__
>     if attr.reverse: attr.update_reverse(obj, None, val, undo_funcs)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 1484, in update_reverse
>     if new_val is not None: reverse.__set__(new_val, obj, undo_funcs)
>   File "<auto generated wrapper of __set__() function>", line 2, in __set__
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/utils.py",
> line 64, in cut_traceback
>     return func(*args, **kwargs)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 1370, in __set__
>     old_val = attr.load(obj)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 1332, in load
>     else: obj._load_()
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 3541, in _load_
>     objects = entity._fetch_objects(cursor, attr_offsets)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 3126, in _fetch_objects
>     obj._db_set_(avdict)
>   File
> "/Users/steph/dev/manager_env/lib/python2.7/site-packages/pony/orm/core.py",
> line 3568, in _db_set_
>     elif old_dbval == new_dbval:
> TypeError: can't compare offset-naive and offset-aware datetimes
> """
>
> IIRC sqlalchemy suffers from that too. Not sure there is an elegant
> solution (obviously the pony doesnt know anything about the tz library
> im using - pytz). So i probably have to stick to naive datetimes.
>
> Cheers,
> Stephane
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140828/01ca0a55/attachment.html>


More information about the ponyorm-list mailing list