简体   繁体   English

在 sqlalchemy 中返回完整的语句

[英]Returning full statement with join in sqlalchemy

I'm using the SQLalchemy table declaration to avoid using strings and manually generating SQL statements.我正在使用 SQLalchemy 表声明来避免使用字符串并手动生成 SQL 语句。 This has worked quite well, except for the following use case where I'm trying to return a statement which creates a merged table with all columns from both tables, joined on an implicit PK/FK which exists between the tables.这工作得很好,除了以下用例,我试图返回一个语句,该语句创建一个合并表,其中包含两个表中的所有列,连接在表之间存在的隐式 PK/FK 上。

It seems like the statement below only selects columns from the first (Result) table and I'm not sure how to generate the full select statement?下面的语句似乎只从第一个(结果)表中选择列,我不确定如何生成完整的 select 语句?

sql = Query(Results, Details).join(Details)\
            .filter(Details.result_type == 'standard')\
            .statement

The query construct isn't the best to use for this use case, as it will return the objects themselves.查询构造不是这个用例的最佳使用,因为它会返回对象本身。 Instead, this can be done with the select construct as follows:相反,这可以通过 select 构造来完成,如下所示:

sql = select([Results, Details])\
        .select_from(Results.join(Details))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM