Ich versuche, eine Abfrage in Python mit SQLAlchemy auszuführen, um alle WLCs in der Tabelle mit diesem Code abzurufen:
Code: Select all
with create_new_session() as session:
statement = select(Location.wlc)
rows = session.exec(statement).all()
Wenn keine Datensätze in der Tabelle vorhanden sind, schlägt der Code fehl.
Unter dem Deckmantel erstellt SQLAlchemy diese Abfrage für mich:
Code: Select all
SELECT "LOCATIONS"."LOCS"."WLC" FROM "LOCATIONS"."LOCS"
Code: Select all
statement = text('SELECT "LOCATIONS"."LOCS"."WLC" FROM "LOCATIONS"."LOCS"')
Code: Select all
statement = text('SELECT LOCS.WLC FROM LOCS')
Die Ausnahme, die ausgelöst wird, ist:
Code: Select all
File "/home/genaro/Documents/worklocations/src/work_locations/jobs/handlers/extract.py", line 240, in _get_db2_wlcs
rows = session.exec(statement).all()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 1476, in all
return self._allrows()
^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 401, in _allrows
rows = self._fetchall_impl()
^^^^^^^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 1389, in _fetchall_impl
return self._real_result._fetchall_impl()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 1813, in _fetchall_impl
return list(self.iterator)
^^^^^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 147, in chunks
fetch = cursor._raw_all_rows()
^^^^^^^^^^^^^^^^^^^^^^
File "/home/genaro/Documents/worklocations/venv/lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 393, in _raw_all_rows
return [make_row(row) for row in rows]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable
Irgendwelche Ideen? Mache ich etwas falsch? Vielen Dank im Voraus.
PS: Vervollständigen Sie meine Frage, um die folgenden Fragen zu beantworten:
- Dies sind die Versionen:
Name: SQLAlchemy
Version: 1.4.52
Name: sqlmodel
Version: 0.0.11
Name: ibm_db
Version: 3.2.7
Name: ibm_db_sa
Version: 0.4.2 - Dies ist create_new_session(). Aber wie gesagt, wenn die Tabelle Datensätze enthält, funktioniert der Code einwandfrei.
Code: Select all
def create_new_session() -> Session:
"""Creates new db2 session"""
engine = create_engine(DB_URL)
session = Session(engine)
return session
Mobile version