How to get Column from Table using Bracket Notation in declarative SQLAlchemy 2.0

Question:

I use python SQLAlchemy 2.0 ORM in declarative style to define my tables like such:

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import select

class Example(DeclarativeBase):
    __tablename__ = 'example'
    foo = mapped_column(Text, nullable=False)

I know that I can access table column in queries using "dot notation":

select(Example.foo).select_from(Example)

Is there some way to also access tables using "bracket notation"? This might look like the following:

select(Example['foo']).select_from(Example)
Asked By: Martin Reindl

||

Answers:

This ended up working for me:

select(Example.__table__.c['foo']).select_from(Example)
Answered By: Martin Reindl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.