On Dec 10, 2013, at 12:00 AM, Matthew Pounsett <matt.pouns...@gmail.com> wrote:

> I'm trying to work with the postgres ARRAY type and I'm having a hard time 
> figuring out what I'm doing wrong with filtering queries on the array column. 
>  Here's some sample code, omitting the session setup:
> 
> class MyTable(Base):                                                          
>   
>    __tablename__ = 'mytable'
>    id = Column(Integer, primary_key=True)                                     
>  
>    myset = Column(sqlalchemy.dialects.postgresql.ARRAY(String))               
>                      
> 
> Base.metadata.create_all(engine)                                              
>   
>                                                                               
>   
> z = ['a', 'b', 'c']                                                           
>                                                                               
>     
> match = session.query(MyTable).\                                              
>   
>         filter(MyTable.myset == z).\                                          
>   
>         all()                                                                 
>                                                                               
>     

that’s a little strange but you can get around it using CAST:

match = session.query(MyTable).\
        filter(MyTable.myset == cast(z, ARRAY(String))).\
        all()

though maybe there’s a better operator to use for array == array comparison, 
not sure


of course if you declare you column like this:

   myset = Column(ARRAY(Text))


it works…..in Postgresql there’s no real difference between TEXT and VARCHAR 
(which is unusual).   Not sure why PG can’t cast between array of VARCHAR vs. 
TEXT but I bet there’s some info on the web about that, it’s unusual PG would 
have a problem with something like that (though a google seems to confirm it).


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to