Hi Mike, On Wednesday, March 31, 2021 at 1:36:33 PM UTC-4 Mike Bayer wrote:
> so this is not what bound parameters are used for in SQL; bound parameters > are a specific construct in the DBAPI driver that only applies to literal > parameters in a statement, that is, strings, numbers and other values > inside of comparisons, values to be passed. they never correspond to > database objects like table or column names nor do they refer to parts of a > SQL statement. > Thanks for the quick response, and thanks for helping me to improve my understanding of bindparams. In my mind, they were a general way of "templating" the query from SQLAlchemy's point of view, but it makes more sense that they map directly to the bound parameter concept from the DBAPI's perspective. > If you are experimenting with baked query, I would strongly advise > bypassing them entirely and upgrading to SQLAlchemy 1.4, where queries are > now cached automatically and the awkwardness of baked queries is no longer > needed. > As you intuited, I'm still on SQLAlchemy 1.3 at the moment; we have existing baked queries that I was doing some maintenance on and I noticed a repeating query in multiple functions and wanted to factor it out. Once it was not working, I had to figure out how to make it work, even though it's not strictly necessary, just out of my own curiosity. > With baked queries, to include your incoming "model" as part of the cache > key, you can add it up front as one of the arguments to cache on: > > baked_query = BAKERY(lambda session: session.query(model), > args=(model, )) > > that will include the class mentioned by "model" as part of the cache > key. I would ensure that "model" is a long lived object , e.g. a mapped > class. If it's an aliased() object, I'd make sure to use the same > aliased() object each time. > Adding an additional positional argument to augment the cache key worked perfectly for my use case. I am indeed using a subclass of my ORM's declarative base, so it is long-lived. Thanks! Scott -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/323d7a55-7246-4b23-9de7-54bf7d1a7779n%40googlegroups.com.