【发布时间】:2022-12-03 06:02:58
【问题描述】:
我需要使用以下 SQL 表达式来实现SQLAlchemy 1.4.41,Postgres 13.6
SELECT book.name,
my_func(book) AS func_result
FROM book WHERE book.name = 'The Adventures of Tom Sawyer';
有没有办法实现这样的 SQL 表达式?
功能如下,我不应该改变它:
create function my_func(table_row anyelement) returns json
我假设将 Book 传递给 func.my_func 是不正确的,因为 SQLAlchemy 将其解压缩到 Book 属性列表(例如 book.id, book.name, book.total_pages)
from db.models import Book
from sqlalchemy import func, select
function = func.my_func(Book)
query = select(Book.name, function).where(Book.name == 'The Adventures of Tom Sawyer')
【问题讨论】:
标签: python sql sqlalchemy