Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sqlalchemy.sql with a Declarative ORM

Tags:

sqlalchemy

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.

like image 707
syllogism_ Avatar asked Dec 10 '25 23:12

syllogism_


1 Answers

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)
like image 76
Denis Otkidach Avatar answered Dec 12 '25 18:12

Denis Otkidach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!