SQLAlchemy 从 create() 打印原始 SQL [英] SQLAlchemy printing raw SQL from create()

查看:57
本文介绍了SQLAlchemy 从 create() 打印原始 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQLAlchemy 尝试 Pylons,我喜欢它,只有一件事,是否可以打印出从 Table() 生成的原始 SQL CREATE TABLE 数据.create() 在它被执行之前?

I am giving Pylons a try with SQLAlchemy, and I love it, there is just one thing, is it possible to print out the raw SQL CREATE TABLE data generated from Table().create() before it's executed?

推荐答案

from sqlalchemy.schema import CreateTable

print(CreateTable(table))

如果您使用声明式语法:

If you are using declarative syntax:

print(CreateTable(Model.__table__))

更新:

由于我已经接受了答案,并且 klenwell 答案 中有重要信息,因此我也会将其添加到此处.

Since I have the accepted answer and there is important information in klenwell answer, I'll also add it here.

您可以通过使用引擎进行编译来获取特定数据库(MySQL、Postgresql 等)的 SQL.

You can get the SQL for your specific database (MySQL, Postgresql, etc.) by compiling with your engine.

print(CreateTable(Model.__table__).compile(engine))

更新 2:

@jackotonye 在评论中添加了一种无需引擎的方法.

@jackotonye Added in the comments a way to do it without an engine.

print(CreateTable(Model.__table__).compile(dialect=postgresql.dialect()))

这篇关于SQLAlchemy 从 create() 打印原始 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆