'Join multiple tables with identical column names in sqlalchemy core - how?
I'd like to join three tables, using sqlalchemy core (v1.4, but with 2.0 syntax). Using the simplest format
jo=s_table.join(l_table).join(f_table)
res=connection.execute(select(jo))
x=res.fetchone()
I get columns with their original name in x, but if the result contains identical names, the first will get the original name, the second will get a '_1' appended to the column name, the third '_2' etc. How can I specify the naming rules of these attributes? In the sqlalchemy's documentation I've found the alias() method, but it is deprecated and will be removed in v2.0 as far as I understand it.
Solution 1:[1]
Now I found something, but I'm not sure, if it is correct:
from sqlaclhemy import alias, LABEL_STYLE_TABLENAME_PLUS_COL
...
alias1 = s_table.alias('s')
alias2 = l_table.alias('l')
alias3 = f_table.alias('f')
jo = alias1.join(alias2).join(alias3)
res = connection.execute(select(jo).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
x = res.fetchone()
At this point I can access the columns as s_, l_, f_*. (don't need to use the full table name, which is the default)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | I have no name |
