2

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 sqlalchemy to query the data but I got an error.

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

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

I get the following error:

Traceback (most recent call last):
  File "/home/nicolas/anaconda3/envs/chatbot_analytics/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-25-bed7e1c6ef62>", line 1, in <module>
    Session.query(tu).first()
  File "/home/nicolas/anaconda3/envs/chatbot_analytics/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1584, in query
    return self._query_cls(entities, self, **kwargs)
AttributeError: 'Table' object has no attribute '_query_cls'

I use version SQLAlchemy==1.3.19. I use a PostgreSQL database.

Is it possible to query the data with the ORM when obtaining the table like this?

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
nbeuchat
  • 6,575
  • 5
  • 36
  • 50
  • 2
    You forgot to instantiate `Session` in `Session.query(tu)`, and so passed the `Table` object as `self` to `Session.query()`. – Ilja Everilä Aug 21 '20 at 13:29
  • Related: https://stackoverflow.com/questions/57759801/attributeerror-type-object-user-has-no-attribute-query-cls – Ilja Everilä Aug 21 '20 at 13:30
  • @IljaEverilä thanks! That was it. The code in the question is actually correct, not the one in my code..! – nbeuchat Aug 21 '20 at 13:32
  • 1
    Could you please edit this to include the actual code that gave the traceback? Iow. a [mcve], so this would make a good target to close similar questions (past and future) with, for example https://stackoverflow.com/questions/42814144/attributeerror-query-cls?noredirect=1&lq=1. It is important to include the definiton of `Session`, so that it is clear that it is the class, not for example a scoped session. – Ilja Everilä Aug 21 '20 at 13:36
  • @IljaEverilä Just edited my question with the imports and how the code was in my code. Feel free to post an answer with your solution – nbeuchat Aug 21 '20 at 13:48

1 Answers1

3

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()
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127