SELECT array_agg((column1, column2)) from table{"(col1_row1_value, col2_row1_value)", "(col1_row2_value, col2_row2_value)"...}>> 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'/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:185: SAWarning: Unicode type received non-unicode bind param value 'y'. (this warning may be suppressed after 10 occurrences) (util.ellipses_string(value),))/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:185: SAWarning: Unicode type received non-unicode bind param value 'x'. (this warning may be suppressed after 10 occurrences) (util.ellipses_string(value),))['{', '"', '(', 'x', ',', 'y', ')', '"', '}']
from sqlalchemy import *
from sqlalchemy.dialects.postgresql import ARRAY
from hasoffers.core.model import Base
from hasoffers.core.model import Session
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True, autoincrement=True)
x = Column(Unicode)
y = Column(Unicode)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True, autoincrement=True)
a_keys = Column(ARRAY(Integer))
col1 = Column(Unicode)
col2 = Column(Unicode)
Base.metadata.bind = Session.bind
Base.metadata.create_all()
Session.add(A(x="x", y="y"))
Session.add(A(x="f", y="j"))
Session.add(A(x="b", y="s"))
Session.add(B(a_keys=[1], col1="qaz", col2="qwe"))
Session.add(B(a_keys=[2,3], col1="zaq", col2="fds"))
Session.add(B(a_keys=[2,3], col1="gtr", col2="ascs"))
Session.commit()
for row in Session.query(A, func.jsonb_object_agg(B.col1, B.col2)).join(B, A.id == func.any(B.a_keys)).group_by(A.id):
print row
Traceback (most recent call last): File "/home/anton/Projects/.../core/hasoffers/core/run/stuff.py", line 33, in <module> for row in Session.query(A, func.jsonb_object_agg(B.col1, B.col2)).join(B, A.id == func.any(B.a_keys)).group_by(A.id): File "/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 86, in instances util.raise_from_cause(err) File "/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File "/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 77, in instances rows = util.unique_list(rows, filter_fn) File "/home/anton/Projects/.venv/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 757, in unique_list if hashfunc(x) not in seenTypeError: unhashable type: 'dict'> <mailto:sqlalchemy+unsub...@googlegroups.com>.
>> <mailto:sqlalchemy+unsub...@googlegroups.com>.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Mike Bayer <mik...@zzzcomputing.com> writes:Sure, here it is:
> Can you show me the model and some sample data ? Is this the JSONB
> datatype ?
import datetime
from sqlalchemy import create_engine, select, Table, Column, Integer, UnicodeText, MetaData
import sqlalchemy.dialects.postgresql as sapg
metadata = MetaData()
users = Table('users', metadata,
Column('id', Integer(), primary_key=True),
Column('name', UnicodeText()),
Column('active', sapg.DATERANGE()),
)
e = create_engine('postgresql://localhost/test')
metadata.create_all(e)
i = users.insert().values(name='myself', active='[2016-10-21,2020-10-22)')
e.execute(i)
.where(users.c.name == 'myself') \
.where(users.c.active.contains(datetime.date.today()))
print(e.execute(q).fetchall())
This works as is with psycopg2 (maybe it was not clear that I was suggesting
an enhancement, not a fix :-).
ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.