ruipacheco
ruipacheco

Reputation: 16422

Printing insert query with SQLAlchemy

I know the if I try to do a select query using the ORM I can use the print method to print the SQL to be generated:

print session.query(MyModel) should return select * from MyModel (the exact SQL may be different but the idea is there).

I now have a piece of code where I create a parent object, loop over some data to create child objects that connect to the parent object with a relationship then call session.add(parent_object) followed by session.commit().

Except this generates an error I don't think it's possible to generate.

Is there a way to view the SQL statement used to insert the data?

The error generated is:

arg = (<class 'sqlalchemy.exc.ProgrammingError'>, ProgrammingError("(pg8000.core.ProgrammingError) (u'ERROR', u'ERROR', u'22001'... too long for type character varying(10)', u'varchar.c', u'624', u'varchar')",), <traceback object at 0x10c98bb00>)

where the value inserted for the only column I have that is a varchar(10) is '7835109626'

Upvotes: 4

Views: 2447

Answers (1)

georgexsh
georgexsh

Reputation: 16624

you could enable sqlalchemy internal logger to output SQL query:

import logging

logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)

Upvotes: 5

Related Questions