On Dec 21, 2013, at 7:05 AM, Sibylle Koczian <[email protected]> wrote:

> Hello,
> 
> I'm using the interval data type in two PostgreSQL databases with intervals 
> ranging from some days to several years. No problems with intervals measured 
> in days, but with all the interval values which can't be correctly converted 
> to datetime.timedelta.
> 
> All the calculations involving intervals are done by functions inside the 
> database, so for any applications I could just use the string representations 
> (display "2 mons" in a textbox, write the same). If I just use one of the 
> Python drivers for PostgreSQL (psycopg2, py-postgresql), no ORM, I can write 
> casts into the SQL as needed. But how can I do this using SQLAlchemy? I tried 
> to declare the column in the mapped table as String, but that doesn't help, 
> the values still are converted to 30 days for a month.


In the case of using Postgresql, the type 
sqlalchemy.dialects.postgresql.INTERVAL takes over wherever you might have used 
a sqlalchemy.Interval type.    In this case, psycopg2 is what’s doing whatever 
conversions are occurring here - if a result row type has the Postgres OID for 
an “INTERVAL”, psycopg2 jumps in and does the conversion to timedelta.  This 
isn’t on the SQLAlchemy side.   If psycopg2 is doing the wrong thing you might 
want to look over on their side for updates or bug reports.

So if what you’re getting at is using CAST, so that by the time the result 
value hits psycopg2 it’s a plain character, SQLAlchemy uses the cast() function 
to achieve that result.

There’s ways to have this cast() embedded into a custom type also, so that it’s 
automatic, if that helps.   Depends on the specifics of how you’re trying to do 
this.




> 
> Thank you for hints,
> Sibylle
> 
> 
> -- 
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to