> Hey Mike and company,
>
> If you were to run the following code against a postgresql 9.5+ database,
> you'll see an exception because the "contains" function sqlalchemy
> associates with the func.tstzrange expression below is intended for an
> Interval type when instead it ought to use the contains for a Timestamp.
> After the code example is the exception that is raised, followed by a query
> you can run to confirm that a Timestamp type is created.
The func.tstzrange() function doesn't have a preset return type in
SQLAlchemy core, you have to specify it:
from sqlalchemy.dialects.postgresql import INTERVAL, TSTZRANGE
from sqlalchemy import (create_engine, select, func, literal, cast)
db_url = "postgres://scott:tiger@localhost/test"
engine = create_engine(db_url, echo=True)
with engine.connect() as conn:
timeslot = func.tstzrange(
(
func.current_date() + cast('8 hours', INTERVAL())
),
(
func.current_date() + cast('12 hours', INTERVAL())
),
'[]',
type_=TSTZRANGE
)
is_ongoing = conn.scalar(
select([literal(True)]).where(timeslot.contains(func.now())))
then the output is:
SELECT %(param_1)s AS anon_1
WHERE tstzrange(CURRENT_DATE + CAST(%(param_2)s AS INTERVAL),
CURRENT_DATE + CAST(%(param_3)s AS INTERVAL), %(tstzrange_1)s) @>
now()
which I think is what you're looking for.
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
>
http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See
http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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.