Getting comparison TypeError when trying to commit "aware" datetime when previous value was "naive" datetime

138 views
Skip to first unread message

Steve Zatz

unread,
May 12, 2012, 6:30:53 PM5/12/12
to sqlalchemy
When I try to commit a timezone aware datetime to replace a value that was previously timezone naive, I get a TypeError when I try to do the commit with the message: 

TypeError: can't compare offset-naive and offset-aware datetimes

Now I am not trying to compare anything but just store the new value in an sqlite database.  Is there a way to force the commit to take place without it performing a comparison to the previously stored value (if that is in fact what is going on)?

Michael Bayer

unread,
May 13, 2012, 6:07:19 AM5/13/12
to sqlal...@googlegroups.com

the ORM needs to compare in order to determine changes, and in general you
should only be dealing with timezone-naive datetimes within a Python
application, I'd suggest ensuring they are all in UTC in fact. It's no
different IMHO from converting encoded strings to unicode first so that
all values are normalized into a common format.
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>

Steve Zatz

unread,
May 13, 2012, 7:28:55 AM5/13/12
to sqlal...@googlegroups.com
> you should only be dealing with timezone-naive datetimes within a Python application
Thanks - make sense although the klugey way around the problem is just to commit None before you change between naive and aware since comparisons with None are fine.


Reply all
Reply to author
Forward
0 new messages