Using sqlalchemy.sql with declarative ORM

The sqlalchemy.sql module seems to suggest that I created the table instances, but I specified my ORM using a declarative style. Is there a way to extract table instances from Declarative classes? using session.query () seems to have stronger syntax than sqlalchemy.sql methods.

+3
source share
1 answer

You can get it from the attribute of __table__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)
+3
source

Source: https://habr.com/ru/post/1726222/


All Articles