tags:

views:

67

answers:

1

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 Declarative classes? using session.query() seems to have worse syntax than the sqlalchemy.sql methods do.

+1  A: 

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)
Denis Otkidach