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.

echo=true with hide_parameters=false not working

See original GitHub issue

Describe the bug I am trying to use sqa.create_engine(conn, echo=True, hide_parameters=False) to log queries with actual parameter values, but it looks like the parameters are not being replaced.

To Reproduce

import sqlalchemy as sqa

echo_engine = sqa.create_engine(
  "postgresql://localhost/postgres",
  echo=True,
  hide_parameters=False
)

echo_engine.execute(sqa.text("select :some_private_name"), {"some_private_name": "1"})

Error

2021-01-19 19:23:07,829 INFO sqlalchemy.engine.base.Engine select version()
2021-01-19 19:23:07,829 INFO sqlalchemy.engine.base.Engine {}
2021-01-19 19:23:07,832 INFO sqlalchemy.engine.base.Engine select current_schema()
2021-01-19 19:23:07,833 INFO sqlalchemy.engine.base.Engine {}
2021-01-19 19:23:07,840 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2021-01-19 19:23:07,840 INFO sqlalchemy.engine.base.Engine {}
2021-01-19 19:23:07,844 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2021-01-19 19:23:07,844 INFO sqlalchemy.engine.base.Engine {}
2021-01-19 19:23:07,845 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2021-01-19 19:23:07,845 INFO sqlalchemy.engine.base.Engine {}
2021-01-19 19:23:07,847 INFO sqlalchemy.engine.base.Engine select %(some_private_name)s
2021-01-19 19:23:07,847 INFO sqlalchemy.engine.base.Engine {'some_private_name': '1'}

I would expect select %(some_private_name)s to be select 1

Versions.

  • OS: MacOS BigSur
  • Python: python3.8
  • SQLAlchemy: 1.3.22
  • Database: postgresql
  • DBAPI: postgresql://

Have a nice day!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
zzzeekcommented, Jan 20, 2021

oh I see you used a psycopg2 object that gives you the parameters embedded. That’s not something that would be available for any other backend, but that’s handy, nice job!

1reaction
tiagotaveiragomescommented, Jan 20, 2021

So I ended up passing a logger at the DBAPI level by building the connection object on the do_connect event handler.

import logging
logging.basicConfig(level=logging.DEBUG)

import psycopg2
import sqlalchemy as sqa

handler = logging.FileHandler(filename="sql.log", mode="w")

sql_logger = logging.getLogger(__name__)
sql_logger.addHandler(handler)

@event.listens_for(sqa.engine.Engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
  cparams["connection_factory"] = psycopg2.extras.LoggingConnection
  conn = psycopg2.connect(*cargs, **cparams)
  conn.initialize(sql_logger)
  return conn

engine = sqa.create_engine(metadata.conn) # Will log the actual queries to the file

df.to_sql(engine, ...)

Thank you for you help understanding this matter!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to show code but hide output in RMarkdown?
This echo=True, include=FALSE here does not work: the whole thing is hidden (which is the normal behavior of include=FALSE ).
Read more >
Execution Options - Quarto
Include the results of executing the code in the output ( true , false , or asis to indicate that the output is...
Read more >
Use runtime and type-safe parameters - Azure Pipelines
In this article. Use parameters in pipelines; Use conditionals with parameters; Parameter data types; FAQ. Azure DevOps Services | Azure ...
Read more >
Parameters - AWS CloudFormation
Use parameters to pass values to your template when creating or updating a stack so that you can customize each stack deployment.
Read more >
2.6 R code chunks and inline R code | R Markdown - Bookdown
When you are trying to set echo = FALSE , results = 'hide' , warning = FALSE , and ... However, I want...
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