开发者

Using sqlalchemy.sql with a Declarative ORM

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜