I'm writing web api which passes SQL response queried via SQL Alchemy. I'd like to retrieve column names of the query WITH the source table.
Here's example:
fields = [
StudentsTable.name,
SubjectsTable.name
]
query = self.session()\
.query(*fields)\
.filter(StudentsTable.id_some_fk == SubjectsTable.id_some_pk)
return {
"meta": {
"fields": query.column_descriptions
},
"data": query.all()
}
The "fields" variable will be dynamic, eg. user will be able to pass it as string list and I'd like to have a unified response from SQLAlchemy, not by iterating the fields variable.
With this query.column_descriptions I get column names as expected. However, those are only column names, without the source table name. So, in this case I get both "name" columns, which I'm not sure which name is it.
{
"meta": {
"fields": [
{
"name": "name",
"type": null,
"aliased": false,
"expr": null,
"entity": null
},
{
"name": "name",
"type": null,
"aliased": false,
"expr": null,
"entity": null
}
]
},
"data": []
}