What's the proper way to return in an ORM query the value of a Postgres 
array attribute at a given specific index within the array?

I have a db table with a column called value, which is a 2d array, defined 
as REAL[][]. 

My ModelClass is defined as 

class EmLine(Base):
    __tablename__ = 'emline'
    __table_args__ = {'autoload': True, 'schema': 'dapdb'}

    def __repr__(self):
        return '<EmLine (pk={0})'.format(self.pk)

    value = Column(ARRAY(Float, dimensions=2, zero_indexes=True))
    ivar = Column(ARRAY(Float, dimensions=2, zero_indexes=True))
    mask = Column(ARRAY(Integer, dimensions=2, zero_indexes=True))


Pure SQL indexing an array works just fine
select e.value[16][17] from dapdb.emline as e;

But SQLalchemy does not
session.query(dapdb.EmLine.value[16][17]).first()

returns the error
NotImplementedError: Operator 'getitem' is not supported on this expression

I've tried defining a hybrid method/expression in my ModelClass, and running
session.query(dapdb.EmLine.singleat('value',16,17)).first()

 but I'm getting the same "getitem" error

class EmLine(Base):
    __tablename__ = 'emline'
    __table_args__ = {'autoload': True, 'schema': 'mangadapdb'}

    def __repr__(self):
        return '<EmLine (pk={0})'.format(self.pk)

    value = Column(ARRAY(Float, dimensions=2, zero_indexes=True))
    ivar = Column(ARRAY(Float, dimensions=2, zero_indexes=True))
    mask = Column(ARRAY(Integer, dimensions=2, zero_indexes=True))

    @hybrid_method
    def singleat(self, name, x, y):
        param = self.__getattribute__(name)
        return param[x][y]

    @singleat.expression
    def singleat(cls, name, x, y):
        param = cls.__getattribute__(cls, name)
        print(param, x, y)
        return func.ARRAY(param)[x][y]


In my singleat expression, I've tried a variety of returns.  return 
func.ARRAY(param)[x][y] ;  return param[x][y].  What's the proper syntax to 
match the actual SQL array indexing?



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to