如何在具有相同列名的sqlalchemy查询中连接不同的表?

时间:2022-10-26 07:56:27

Description of the tables are:

表的说明如下:

License: id, customer_id, product_id, expires_at
Customer: id, name
Product: id, name

I am querying like this:

我是这样问的:

result = session.\
            query(License.id, License.customer_id, License.product_id, License.status, License.expires_at,\
                  Customer.name,\
                  Product.name).\
            # some filtering on those columns (JOIN conditions)
            all()

I want the joined table to contain:

我希望合并后的表包含:

License.id, Customer.name, Product.name

Now the result I am getting is a list of KeyedTuples. How can I access the required columns from those? e.g. result[0].name gives only Customer.name, then how to get Product.name as well?

现在我得到的结果是一个KeyedTuples列表。如何从这些列中访问所需的列?结果[0].name只提供Customer.name,然后如何获得产品。

1 个解决方案

#1


4  

Use the label method:

使用标签的方法:

Customer.name.label("Customer_name"),\
Product.name.label("Product_name").\

#1


4  

Use the label method:

使用标签的方法:

Customer.name.label("Customer_name"),\
Product.name.label("Product_name").\