On 01/06/2017 09:48 AM, Alexander O'Donovan-Jones wrote:
Does anyone know what the syntax for using DateRanges is?

I'm trying to find out if a given date falls within a range, and so
thought that the `in` syntax in Python might work (to no avail):

from psycopg2.extras import DateRange
session.query(Order).filter(Order.created_at in DateRange(d1, d2, '[]'))

I then realised I was using incorrect types for this, and so cast my
DATETIME column to a DATE:

session.query(Order).filter(cast(Order.created_at, DATE) in
DateRange(d1, d2, '[]'))

However this also doesn't work, and throws the exception `TypeError:
Boolean value of this clause is not defined`

I did manage to get it working, but the syntax is clunky, and so was
wondering if there's something I'm missing.

session.query(Order).filter(cast(Order.created_at,
DATE).op('<@')(DateRange(d1, d2, '[]')))

which explicitly uses the ' contained by' operator.

First off, Python's "in" operator is not related to SQLAlchemy expressions at all. If you did want to use IN, you'd be using the in_() method, e.g. column.in_(). But I don't think that will work here.

However, if you are looking for the contained by operator here, your usage of op('<@') is probably best as I don't think we have a "contained_by" operator outside of the JSON data types.




--
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to