question :)

10 views
Skip to first unread message

Massimiliano della Rovere

unread,
Oct 9, 2020, 8:52:30 AM10/9/20
to sqlal...@googlegroups.com
Greetings,
is it possible using sqlalchemy core to obtain the following code:

[...]
WHERE (column1, column2) = (value1, value2)

this is useful to use multi columnar indexes having column1 and column2 in the two leftmost position and in later position columns that I put in the SELECT section. 

Mike Bayer

unread,
Oct 9, 2020, 9:10:13 AM10/9/20
to noreply-spamdigest via sqlalchemy
yes the tuple construct provides this:


>>> from sqlalchemy import select, column, tuple_
>>> stmt = select([column('q')]).where(tuple_(column('x'), column('y')) == tuple_(3, 4))
>>> print(stmt)
SELECT q
WHERE (x, y) = (:param_1, :param_2)


however, tuples are not supported on all backends and it's safer to use AND:

>>> from sqlalchemy import and_
>>> stmt = select([column('q')]).where(and_(column('x') == 3, column('y') == 4))
>>> print(stmt)
SELECT q
WHERE x = :x_1 AND y = :y_1

From a Python perspective, there's no real reason to use one or the other, as either one can be generated programmatically.

the tuple_ construct is provided mostly to support IN expressions against composite values, for equality it's not strictly necessary.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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.

Reply all
Reply to author
Forward
0 new messages