Ok, I'l try it, thank you. вторник, 11 апреля 2017 г., 17:26:18 UTC+3 пользователь Mike Bayer написал: > > I will say that the psycopg2 driver is not supporting this, however, it > isn't parsing out the tuple. Using postgresql.ARRAY we get an answer > like: > > ['{', '"', '(', 'x', ',', 'y', ')', '"', ',', '"', '(', 'x', ',', 'y', > ')', '"', '}'] > > that is, the string coming back is being interpreted as an array by > SQLAlchemy, which here it is not. you might need to use some of the > psycopg2 extension points to support this like > > http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.new_array_type. > > > > > > On 04/11/2017 09:43 AM, mike bayer wrote: > > can't reproduce (though the ARRAY(unicode) type is not what psycopg2 > > returns, and there seems to be a difference in behavior between > > sqlalchemy.ARRAY and sqlalchemy.dialects.postgresql.ARRAY). > > > > please provide a complete example based on the below test script and > > stack traces > > > > also my PG database doesn't know about the jsonb_object_agg function > > > > from sqlalchemy import * > > from sqlalchemy.orm import * > > from sqlalchemy.ext.declarative import declarative_base > > > > Base = declarative_base() > > > > > > class A(Base): > > __tablename__ = 'a' > > id = Column(Integer, primary_key=True) > > x = Column(Unicode) > > y = Column(Unicode) > > > > e = create_engine("postgresql://scott:tiger@localhost/test", echo=True) > > Base.metadata.drop_all(e) > > Base.metadata.create_all(e) > > > > s = Session(e) > > s.add(A(x="x", y="y")) > > s.commit() > > > > row = s.query(func.array_agg(tuple_(A.x, A.y), > > type_=ARRAY(Unicode))).scalar() > > print row > > > > > > > > > > > > On 04/11/2017 09:04 AM, Антонио Антуан wrote: > >> Hi > >> I want to build such query with sqlalchemy: > >> | > >> SELECT array_agg((column1,column2))fromtable > >> | > >> > >> Using psql it works perfectly and returns such result: > >> | > >> {"(col1_row1_value, col2_row1_value)","(col1_row2_value, > >> col2_row2_value)"...} > >> | > >> > >> I tried several forms of SQLA-query: > >> > >> |>> from sqlalchemy.dialects.postgresql import ARRAY > >>>> from sqlalchemy.sql.elements import Tuple > >>>> ... > >>>> func.array_agg(Tuple(Model.col1,Model.col2),type_=ARRAY(Unicode))... > >> ... > >> TypeError: unhashable type: 'list' > >>>> ... func.array_agg(Tuple(Model.col1, Model.col2), > type_=ARRAY(Unicode, > >> as_tuple=True))... > >> ...# returns value like this: ('{', '"', '(', 'c',...) > >>>> ... func.array_agg(Tuple(Model.col1, Model.col2), type_=ARRAY(Tuple, > >> as_tuple=True))... > >> ... > >> AttributeError: Neither 'Tuple' object nor 'Comparator' object has an > >> attribute 'dialect_impl' > >> | > >> > >> At first, I wanted to use /`func.jsonb_object_agg(Model.col1, > >> Model.col2)`/, but it raises */"unhashable type: dict"/* > >> */ > >> /* > >> Could you point to solution? > >> > >> -- > >> SQLAlchemy - > >> The Python SQL Toolkit and Object Relational Mapper > >> > >> http://www.sqlalchemy.org/ > >> > >> To post example code, please provide an MCVE: Minimal, Complete, and > >> Verifiable Example. See http://stackoverflow.com/help/mcve for a full > >> description. > >> --- > >> 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+...@googlegroups.com <javascript:> > >> <mailto:sqlalchemy+unsubscr...@googlegroups.com <javascript:>>. > >> To post to this group, send email to sqlal...@googlegroups.com > <javascript:> > >> <mailto:sqlal...@googlegroups.com <javascript:>>. > >> Visit this group at https://groups.google.com/group/sqlalchemy. > >> For more options, visit https://groups.google.com/d/optout. >
-- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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.