使用SQLAlchemy创建存储过程

Tar*_*ngh 6 postgresql stored-procedures sqlalchemy

我正在编写一个python脚本来使用SQLAlchemy创建postgres数据库.我也想以同样的方式创建存储过程.我检查了SQL Alchemy Documentations但是无法找到是否可以使用它创建存储过程.有可能这样做吗?任何教程/示例都会有所帮助.我找到了一些如何使用SQLAlchemy调用SP而不是如何创建SP的示例.

提前致谢.塔拉辛格

van*_*van 9

You can create stored procedures (actually, execute any valid SQL statement) by using sqlalchemy.sql.expression.text construct:

t = text("SELECT * FROM users WHERE id=:user_id")
result = connection.execute(t, user_id=12)
Run Code Online (Sandbox Code Playgroud)

But this will be more of an appendix to the SQLAlchemy rather than designed usage.
Also this cannot be done in a DMBS-independent way, which is one of the benefits using ORM tools like SQLAlchemy.
If your aim is to version-control your database schema, you still can use it, but you need to take complete control this process and handle things like dependencies between stored procedures, UDFs, views and create/drop them in the proper order.