如何从SqlAlchemy获取原始SQL

时间:2019-07-19 22:33:27

标签: postgresql sqlalchemy flask-sqlalchemy

我有以下代码,可将多个行批量插入表中。

session = SessionManager.maker()
session.bulk_insert_mappings(mappings=insertion_dict_values, mapper=self.model)
session.commit()
session.close()

我想查看bulk_insert_mapping生成的原始sql语句。我尝试设置echo = True:

create_engine(db_url, echo=True)

我从日志中获得的最好成绩是:

INSERT INTO bogs (created_at, updated_at, address, foo, bar, baz) VALUES (%(created_at_m0)s, %(updated_at_m0)s, %(address_m0)s, %(foo_m0)s, %(bar_m0)s, %(baz_m0)s), (%(created_at_m1)s, %(updated_at_m1)s, %(address_m1)s, %(foo_m1)s, %(bar_m1)s, %(baz_m1)s), (%(created_at_m2)s, %(updated_at_m2)s, %(address_m2)s, %(foo_m2)s, %(bar_m2)s, %(baz_m2)s), (%(created_at_m3)s, %(updated_at_m3)s, %(address_m3)s, %(foo_m3)s, %(bar_m3)s, %(baz_m3)s), (%(created_at_m4)s, %(updated_at_m4)s, %(address_m4)s, %(foo_m4)s, %(bar_m4)s, %(baz_m4)s), (%(created_at_m5)s, %(updated_at_m5)s, %(address_m5)s, %(foo_m5)s, %(bar_m5)s, %(baz_m5)s), (%(created_at_m6)s, %(updated_at_m6)s, %(address_m6)s, %(foo_m6)s, %(bar_m6)s, %(baz_m6)s), (%(created_at_m7)s, %(updated_at_m7)s, %(address_m7)s, %(foo_m7)s, %(bar_m7)s, %(baz_m7)s), (%(created_at_m8)s, %(updated_at_m8)s, %(address_m8)s, %(foo_m8)s, %(bar_m8)s, %(baz_m8)s), (%(created_at_m9)s, %(updated_at_m9)s, %(address_m9)s, %(foo_m9)s, %(bar_m9)s, %(baz_m9)s) RETURNING bogs.created_at, bogs.updated_at, bogs.id, bogs.address, bogs.foo, bogs.bar, bogs.baz

这看起来很接近原始sql语句,但还不完全到此(即值应该已经插值了)。

是否有一种方法可以获取发送到postgres的实际sql语句?

0 个答案:

没有答案