The sqlalchemy.sql module seems to assume that I've created Table instances, but I've specified my ORM using the Declarative style. Is there a way to extract the table instances from the D开发者_高级运维eclarative classes? using session.query() seems to have worse syntax than the sqlalchemy.sql methods do.
You can get it from __table__
attribute of your model class:
# Some code is omitted
class Model(Base):
__tablename__ = 'models'
id = Column(Integer, primary_key=True)
print repr(Model.__table__)
Outputs:
Table('models', MetaData(None), Column('id', Integer(), table=<models>,
primary_key=True, nullable=False), schema=None)
精彩评论