[ad_1]
I’m using python 3.8, sqlalchemy 1.4.21 & PostgreSQL 12.8.
I have no idea how to access the elements of list (array) gotten from postgres-function.
It’s easy to get the elements of list from Column object, but in the case of ColumnClause object, the same method didn’t work.
from sqlalchemy import Colum, Integer, String, ARRAY, func
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
session = Session(autocommit=False, autoflush=True, bind=engine)
Base = declarative_base()
class Process(Base):
__tablename__ = 'process'
id = Column(Integer(), primary_key=True)
product = Column(String(100))
processes = Column(ARRAY(String()))
process = Process
q1 = session.query(
process.id
, process.product
, process.processes[1] # <- It's OK.
, func.array_positions(process.processes, 'process_A').label('proc_idx')
).select_from(process).cte('q1')
q2 = session.query(
q1.c.id
, q1.c.product
, q1.c.proc_idx[1] # <- Error has occurred here!!
).select_from(q1).order_by(q1.c.id)
Error
NotImplementedError
Operator ‘getitem’ is not supported on this expression
To my understanding, “q1.c.proc_idx” (gotten from postgres-function: func.array_positions) is ColumnClause object and getitem operator (square brackets of list[]) is not implemented in ColumnClause object.
On the other hand, “process.proccesses[1]” is valid syntax because getitem operator is implemented in Column object.
How do I get the elements of list gotten from postgres-function?
Thanks in advance.
[ad_2]