question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unique bindparam for text

See original GitHub issue

Hello, I have an issue with sa.text with parameters and sa.union_all function.

query = sa.text('select id from runs where id=:run_id').columns(id=sa.Integer)

queries = []

queries.append(query.bindparams(run_id=433))
queries.append(query.bindparams(run_id=432))

conn.execute(queries[0]).fetchall()
# OK: [(433,)]

conn.execute(queries[1]).fetchall()
# OK: [(432,)]

q_union = sa.union_all(*queries)

conn.execute(q_union).fetchall()
# NOK: [(432,), (432,)]
#      the same ids, it seems last parameter applied to all queries
# EXPECTED: [(433,), (432,)]

# BUT:
print(q_union.compile(compile_kwargs={"literal_binds": True}))
# OK: select id from runs where id=433 UNION ALL select id from runs where id=432
#     different ids as set in bindparams

db details:

runs = sa.Table(
    'runs', metadata,

    Column('id', sa.Integer, primary_key=True),
    # ...
)
CONNECTION_STRING = 'postgresql://user:pass@localhost/db'

bindparams example I found in documentation: https://docs.sqlalchemy.org/en/13/core/tutorial.html#specifying-bound-parameter-behaviors

I use: sqlalchemy==1.3.5 psycopg2==2.7.5 python==3.6.8 postgres:11.1

PS: In production my queries much complex than in example. I use sa.select(run.c.id).where(…) and so on, but I have part of query with sa.text with parameters and have unexpected behavior above. Example of real parts:

sa.select(
    ..., sa.text(':key as key').bindparams(key=key)
).where(
    ... & sa.text("(runs.partial_ready->>:key)::bool = true").bindparams(key=key)
)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sqla-testercommented, Oct 23, 2019

Mike Bayer has proposed a fix for this issue in the rel_1_3 branch:

Support unique bound parameters for text() https://gerrit.sqlalchemy.org/1541

0reactions
sqla-testercommented, Oct 23, 2019

Mike Bayer has proposed a fix for this issue in the master branch:

Document unique bound parameters for text() https://gerrit.sqlalchemy.org/1540

Read more comments on GitHub >

github_iconTop Results From Across the Web

PDOStatement::bindParam - Manual - PHP
Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement....
Read more >
Difference between bindParam and bindValue in PHP
The bindParam () function is used to pass variable not value. bindParam() function is executed at runtime. bindParam is a PHP inbuilt function....
Read more >
How to only bindParam if POST values are not empty
When I put an input into the text field and click submit, this new value is not in the database (the field first...
Read more >
Column Elements and Expressions
and_(); bindparam(); case(); cast(); column(); custom_op; distinct() ... function sqlalchemy.sql.expression.column(text, type_=None, is_literal=False, ...
Read more >
How to insert data using bindParam() in pdo using php mysql
In this video, I have taught how to insert data into database using bindParam () function in pdo using php mysql.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found