Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join different tables in sqlalchemy query having same column names?

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?

like image 687
exAres Avatar asked Aug 05 '14 08:08

exAres


1 Answers

Use the label method:

Customer.name.label("Customer_name"),\
Product.name.label("Product_name").\
like image 157
Barmar Avatar answered Oct 09 '22 05:10

Barmar