Skip to content
Advertisement

How to have the possibility to call name of columns in db.session.query with 2 tables in Flask Python?

I am developing a web application with Flask, Python, SQLAlchemy, and Mysql.

I have 2 tables:

JavaScript

I need to extract all the tasksusers (from TaskUser) where the id_task is in a specific list of Task ids. For example, all the taskusers where id_task is in (1,2,3,4,5)

Once I get the result, I do some stuff and use some conditions.

When I make this request :

JavaScript

I get this output error:

JavaScript

It is normal as my SQLAlchemy request is calling only one table. I can’t access to columns of table Task.

BUT I CAN call the columns of TaskUser by their names (see item.id_task).

So I change my SQLAlchemy to this:

JavaScript

This time I include the table Task in my query BUT I CAN’T call the columns by their names. I should use the [index] of columns. I get this kind of error message:

JavaScript

The problem is I have many more columns (around 40) on both tables. It is too complicated to handle data with index numbers of columns.

I need to have a list of rows with data from 2 different tables and I need to be able to call the data by column name in a loop.

Do you think it is possible?

Advertisement

Answer

The key point leading to the confusion is the fact that when you perform a query for a mapped class like TaskUser, the sqlalchemy will return instances of that class. For example:

JavaScript

However, if you specify only specific columns, you will receive just a (special) list of tuples:

JavaScript

If you switch your mindset to completely use the ORM paradigm, you will work mostly with objects. In your specific example, the workflow could be similar to below, assuming you have relationships defined on your models:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement