[sqlalchemy] SQLAlchemy returns me a DATETIME object but the field is a TIMESTAMP

2,258 views
Skip to first unread message

Filippo Spiga

unread,
May 12, 2010, 10:50:41 AM5/12/10
to sqlalchemy
Dear all, I'm a bit confused about this fact. I create a table with
TIMESTAMP fields. The, I execute a SELECT. The formated objects that
SQLAlchemy return me are python DATETIME, not a "timestamp".Is it
normal?

cheers

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

Michael Bayer

unread,
May 12, 2010, 10:56:52 AM5/12/10
to sqlal...@googlegroups.com
On May 12, 2010, at 10:50 AM, Filippo Spiga wrote:

Dear all, I'm a bit confused about this fact. I create a table with
TIMESTAMP fields. The, I execute a SELECT. The formated objects that
SQLAlchemy return me are python DATETIME, not a "timestamp".Is it
normal?

not sure what the question is here.

TIMESTAMP will always generate TIMESTAMP on the DDL side if you use 0.6.

In Python, you only have datetime.datetime() objects as actual values.  There is no "TIMESTAMP" value-holding object in Python.




Filippo Spiga

unread,
May 13, 2010, 5:35:42 PM5/13/10
to sqlalchemy
On 12 Mag, 16:56, Michael Bayer <mike...@zzzcomputing.com> wrote:
> not sure what the question is here.
>
> TIMESTAMP will always generate TIMESTAMP on the DDL side if you use 0.6.
>
> In Python, you only have datetime.datetime() objects as actual values.  There is no "TIMESTAMP" value-holding object in Python.

So, I can define a table field as TIMESTAMP. Put an integer (a good
one, rounding a float) during my INSERT operation and then, during a
SELECT, SQLAlchemy (I'm using the 0.5.8) returns me a python DATETIME
object. And finallyI have to convert it to an INTEGER using
datetime.datetime methods (or time methods, I don't remember now). I
understand that. Is it ok? Is it reasonable?

At this point, can I store directly an integer value. It is probably
faster, isn't it?

Cheers

Michael Bayer

unread,
May 13, 2010, 7:19:11 PM5/13/10
to sqlal...@googlegroups.com

On May 13, 2010, at 5:35 PM, Filippo Spiga wrote:

> On 12 Mag, 16:56, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> not sure what the question is here.
>>
>> TIMESTAMP will always generate TIMESTAMP on the DDL side if you use 0.6.
>>
>> In Python, you only have datetime.datetime() objects as actual values. There is no "TIMESTAMP" value-holding object in Python.
>
> So, I can define a table field as TIMESTAMP. Put an integer (a good
> one, rounding a float) during my INSERT operation and then, during a
> SELECT, SQLAlchemy (I'm using the 0.5.8) returns me a python DATETIME
> object. And finallyI have to convert it to an INTEGER using
> datetime.datetime methods (or time methods, I don't remember now). I
> understand that. Is it ok? Is it reasonable?
>
> At this point, can I store directly an integer value. It is probably
> faster, isn't it?

sure store an int if that's the way you're dealing with time values. You can also wrap the TIMESTAMP() type using TypeDecorator and convert from datetime to int (and back).

Filippo Spiga

unread,
May 14, 2010, 7:15:20 AM5/14/10
to sqlalchemy
On 14 Mag, 01:19, Michael Bayer <mike...@zzzcomputing.com> wrote:
> sure store an int if that's the way you're dealing with time values.    You can also wrap the TIMESTAMP() type using TypeDecorator and convert from datetime to int (and back).

I don't know very well TypeDecorator. Do you have an easy example for
my specific case?

Many thanks!

Michael Bayer

unread,
May 14, 2010, 9:35:09 AM5/14/10
to sqlal...@googlegroups.com

On May 14, 2010, at 7:15 AM, Filippo Spiga wrote:

> On 14 Mag, 01:19, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> sure store an int if that's the way you're dealing with time values. You can also wrap the TIMESTAMP() type using TypeDecorator and convert from datetime to int (and back).
>
> I don't know very well TypeDecorator. Do you have an easy example for
> my specific case?


import datetime
import time
from sqlalchemy import types

class IntTimestampType(types.TypeDecorator):

impl = types.TIMESTAMP

def process_bind_param(self, value, dialect):
return datetime.datetime(*(time.localtime(value)[0:6]))

def process_result_value(self, value, dialect):
if value is not None:
value = time.mktime(value.timetuple())
return value
Reply all
Reply to author
Forward
0 new messages