databases icon indicating copy to clipboard operation
databases copied to clipboard

SQLAlchemy weird behavior with tuples and "render_postcompile"

Open skuda opened this issue 3 years ago • 0 comments

Hello,

The issue is very well explained here: https://github.com/sqlalchemy/sqlalchemy/issues/6114

It's very easy to reproduce it, like this:

from sqlalchemy.sql import table, column, select, tuple_

t = table('test', column('id', sa.Integer()), column('other', sa.Boolean()))
s = select(t).where( tuple_(t.c.id, t.c.other).in_( ((10, True), (20, False))) )

compiled = s.compile(compile_kwargs={"render_postcompile": True})
# str(compiled) = 'SELECT test.id, test.other \nFROM test \nWHERE (test.id, test.other) IN ((:param_1_1_1, :param_1_1_2), (:param_1_2_1, :param_1_2_2))'
# compiled.params = {'param_1_1_1': (10, True), 'param_1_1_2': (20, False)}

So when databases try to create the final SQL applying the params, some names are not found in the formatting because the parameters are expanded in the string but not in the params dictionary.

A solution is available in the linked sqlalchemy issue, not sure if it's the best way to handle it but it should work at least.

skuda avatar Mar 29 '22 13:03 skuda