Skip to content
Advertisement

get table columns from sqlAlchemy table model

I have a table where I would like to fetch all the column names however after browsing the interwebs I could not find a way that works. This is what my table looks like:

JavaScript

I would like to be able to print all the column names from a for loop. ex:

JavaScript

This is pretty easy with regular sql but I can’t seem to figure out how to do it using sqlAlchemy table models

Advertisement

Answer

You get all of the columns from __table__.columns:

JavaScript

or

JavaScript

The columns would be in format myTable.col1 (table name is included). If you want just column names, get the .key for each column:

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