SQLAlchemy introspect column type with inheritance

前端 未结 3 491
自闭症患者
自闭症患者 2021-02-12 15:06

Considering this code (and using SQLAlchemy 0.7.7):

class Document(Base):
    __tablename__ = \'document\'
    __table_args__ = {
        \'schema\': \'app\'
            


        
3条回答
  •  无人及你
    2021-02-12 15:41

    You can explore the base classes...

    def find_type(class_, colname):
        if hasattr(class_, '__table__') and colname in class_.__table__.c:
            return class_.__table__.c[colname].type
        for base in class_.__bases__:
            return find_type(base, colname)
        raise NameError(colname)
    
    print find_type(Arrete, 'date_arrete')
    print find_type(Arrete, 'date')
    

提交回复
热议问题