what is the way to achieve automatic conversion on each query?

16 views
Skip to first unread message

Jinghui Niu

unread,
Jul 25, 2015, 5:21:44 PM7/25/15
to sqlalchemy
I'm using two columns to store my datetime records: one column stores utc timestamp as a string, the other column stores a timezone offset as an integer. Now I find myself most of the time writing ad hoc functions to convert those UTC times to various local times. My code base has become really inconsistent and repetitive.

I'm looking to write some code with sqlalchemy, natively sqlalchemy, to allow me to get the converted local time on each query, automatically. I've heard of that feature before.
I'm kind of lost in the documentation. Could someone point a general direction here? I don't expect too much, just a general direction would be highly appreciated. Thanks a lot.

Jinghui Niu

unread,
Jul 25, 2015, 6:38:55 PM7/25/15
to sqlalchemy, niuji...@gmail.com
By the way, the database driver that I'm using is SQLite, which doesn't support native timestamp I believe.

Mike Bayer

unread,
Jul 26, 2015, 2:06:21 PM7/26/15
to sqlal...@googlegroups.com
--

there are multiple documented techniques for this kind of thing.  Have you looked into composite attributes or hybrid properties ?  Both can suit this use case.





You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Jinghui Niu

unread,
Jul 26, 2015, 4:40:01 PM7/26/15
to sqlal...@googlegroups.com
Thanks Mike,

I've read the documentation on both, hybrid attributes seem to be easier and have a clearer logic in this use case. Although this is just my gut feeling as a layman. What is the rationale behind having two competing solutions?

Also, I dimly remember someone recommended using "property method", I didn't quite understand it back then, does it relate to the two techniques you suggested here?

--
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/qWh8o5bGzU4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.

Mike Bayer

unread,
Jul 26, 2015, 10:46:48 PM7/26/15
to sqlal...@googlegroups.com


On 7/26/15 4:39 PM, Jinghui Niu wrote:
Thanks Mike,

I've read the documentation on both, hybrid attributes seem to be easier and have a clearer logic in this use case. Although this is just my gut feeling as a layman. What is the rationale behind having two competing solutions?

Also, I dimly remember someone recommended using "property method", I didn't quite understand it back then, does it relate to the two techniques you suggested here?
composites are a much older feature and very specific to the multiple-columns use case, but should work here.  hybrids are more open ended, more flexible and probably easier to understand.   sounds like a "property method" to me, e.g. a Python descriptor.
Reply all
Reply to author
Forward
0 new messages