Debugging (displaying) SQL command sent to the db by SQLAlchemy

Posted by morpheous on Stack Overflow See other posts from Stack Overflow or by morpheous
Published on 2010-06-01T14:01:50Z Indexed on 2010/06/01 14:03 UTC
Read the original article Hit count: 148

Filed under:
|

I have an ORM class called Person, which wraps around a person table:

After setting up the connection to the db etc, I run the ff statement.

people = session.query(Person).all()

The person table does not contain any data (as yet), so when I print the variable people, I get an empty list.

I renamed the table referred to in my ORM class People, to people_foo (which does not exist).

I then run the script again. I was surprised that no exception was thrown when attempting to access a table that does not exist.

I therefore have the following 2 questions:

  1. How may I setup SQLAlchemy so that it propagates db errors back to the script?
  2. How may I view (i.e. print) the SQL that is being sent to the db engine

If it helps, I am using PostgreSQL as the db

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy