Skip to content Skip to sidebar Skip to footer

Sqlalchemy - 'table' Object Has No Attribute '_query_cls' When Querying A Table Obtained With Reflect

I am trying to query a table with SQL Alchemy ORM that I connected to using reflect (it is an existing database). I tried to use the method described here: How to query a table, in

Solution 1:

In

Session.query(table)

you are not calling a method of a Session instance, but passing the Table object as the self argument, because Session is the class, not an instance. The usual way to make a pre-configured session is to use sessionmaker to create a tailored version of the Session class, and then instantiate it:

from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker

engine = create_engine(db_uri)
metadata = MetaData(engine)
metadata.reflect()
table = metadata.tables["events"]
Session = sessionmaker(bind=engine)
session = Session()
session.query(table).all()

Post a Comment for "Sqlalchemy - 'table' Object Has No Attribute '_query_cls' When Querying A Table Obtained With Reflect"