How to get column names from SQLAlchemy result (declarative syntax)

后端 未结 7 589
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 01:26

I am working in a pyramid project and I\'ve the table in SQLAlchemy in declarative syntax

\"\"\"models.py\"\"\"
class Projects(Base):
    __tablename__ = \'proje         


        
相关标签:
7条回答
  • 2021-02-01 02:10
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import (Column, Index, Date, DateTime, Numeric, BigInteger, String, ForeignKey, Boolean)
    
    Base = declarative_base()
    
    class Project(Base):
        """sqlalchemy ORM for my table."""
        __tablename__ = "table1"
        id = Column("id", BigIntegerID, primary_key=True, autoincrement=True)
        date = Column("date", Date, nullable=False)
        value = Column("value", Numeric(20, 8))
        ...
        ...
    

    Then this will return the columns names ['id', 'date', 'value', ...]:

    Project.__table__.columns.keys()
    

    Or this

    Project.metadata.tables['table1'].columns.keys()
    
    0 讨论(0)
提交回复
热议问题