我正在使用Flask-SQLAlchemy,我的表/模型结构如下:
class TableName (db.Model):
columnname = db.Column(db.String)
如何在SQLAlchemy中添加Postgres自定义函数?
我指的是像这样的一个函数:
create function canonicalize (text) returns text as $$
select $1;
$$ language sql;
我正在使用Flask-SQLAlchemy,我的表/模型结构如下:
class TableName (db.Model):
columnname = db.Column(db.String)
create function canonicalize (text) returns text as $$
select $1;
$$ language sql;
from sqlalchemy import create_engine, text
engine = create_engine('/path/to/db...')
create_procedure = text("""
CREATE OR REPLACE FUNCTION calculate_sum(a integer, b integer)
RETURNS integer AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
""")
with engine.connect() as connection:
connection.execute(create_procedure)
connection.commit()