Is it possible to use bindparam() for whole where expression?

22 views
Skip to first unread message

Andrei Pashkin

unread,
Jan 20, 2020, 1:46:29 PM1/20/20
to sqlalchemy
Hello!

I wonder - is it possible to parametrize where expression as a whole like that: 
select([users_table]).where(bindparam('criteria'))

?

My use case is that I'd like to pre-compile a complex SQLAlchemy expression to avoid redundant computations and achieve the same effect as Bakery has for the ORM but only for a low-level SA expression.


Mike Bayer

unread,
Jan 20, 2020, 2:27:13 PM1/20/20
to noreply-spamdigest via sqlalchemy


On Mon, Jan 20, 2020, at 1:46 PM, Andrei Pashkin wrote:
Hello!

I wonder - is it possible to parametrize where expression as a whole like that: 
select([users_table]).where(bindparam('criteria'))

nope, that's not how SQL bound parameters work.


?

My use case is that I'd like to pre-compile a complex SQLAlchemy expression to avoid redundant computations and achieve the same effect as Bakery has for the ORM but only for a low-level SA expression.

Wait for SQLAlchemy 1.4, this will have that feature.








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

Mike Bayer

unread,
Jan 20, 2020, 2:29:10 PM1/20/20
to noreply-spamdigest via sqlalchemy


On Mon, Jan 20, 2020, at 2:26 PM, Mike Bayer wrote:


On Mon, Jan 20, 2020, at 1:46 PM, Andrei Pashkin wrote:
Hello!

I wonder - is it possible to parametrize where expression as a whole like that: 
select([users_table]).where(bindparam('criteria'))

nope, that's not how SQL bound parameters work.


?

My use case is that I'd like to pre-compile a complex SQLAlchemy expression to avoid redundant computations and achieve the same effect as Bakery has for the ORM but only for a low-level SA expression.

Wait for SQLAlchemy 1.4, this will have that feature.

noting also, there is a cache feature for Core statements right now, you just have to use the same statement object each time, since currently the statement object itself is the cache key.  See https://docs.sqlalchemy.org/en/13/core/connections.html?highlight=compiled_cache#sqlalchemy.engine.Connection.execution_options.params.compiled_cache .  This is what BakedQuery ultimately uses also and the unit of work uses it also.

In 1.4 all statements will be able to generate a separate cache key that is repeatable for multiple instances of the same statement.

Andrei Pashkin

unread,
Jan 21, 2020, 6:31:14 AM1/21/20
to sqlalchemy
Alright, thank you very much! Very informative.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages