Get table columns from sqlAlchemy table

I have a table where I would like to get all the column names, but after looking at the firewalls, I could not find a way that works. This is what my table looks like:

class myTable(Base): __tablename__ = 'myTable' col1 = Column(Integer, primary_key=True) col2 = Column(Unicode(10)) col3 = Column(Integer) col4 = Column(Numeric(10, 6)) col5 = Column(Numeric(6,3)) col6 = Column(Numeric(6,3)) child = relationship('tChild', backref=backref('children')) 

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

 "col1", "col2", "col3".... etc 

This is pretty easy with regular sql, but I can't figure out how to do this using sqlAlchemy table tables.

+7
python pyramid sqlalchemy
source share
1 answer

You get all columns from __table__.columns :

 myTable.__table__.columns 

or

 myTable.__table__.c 

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

 [column.key for column in myTable.__table__.columns] 
+9
source share

All Articles