The hash() is a built-in python method, used to return a unique number . This can be applied to any user-defined object which won’t get changed once initialized. This property is used mainly in dictionary keys .
TypeError: unhashable type <http://net-informations.com/python/iq/unhashable.htm>: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed. The standard way to solve this issue is to cast a list to a tuple . On Wednesday, October 13, 2010 at 11:52:02 PM UTC+5:30, Julien Demoor wrote: > > Hello, > > The problem I'm seeing is illustrated by the code below. I tried a > workaround using TypeDecorator with process_result_value returning a > tuple rather than a list, to no avail. > > Any help will be greatly appreciated. > > Regards. > > Traceback : > > Traceback (most recent call last): > File "satest2.py", line 23, in <module> > session.query(Foo, 'col').from_statement("SELECT 55 AS foo_bar, > '{1,2,3}'::integer[] AS col;").first() > File "/home/user/programs/env/lib/python2.6/site-packages/sqlalchemy/ > orm/query.py", line 1494, in first > ret = list(self)[0:1] > File "/home/user/programs/env/lib/python2.6/site-packages/sqlalchemy/ > orm/query.py", line 1682, in instances > rows = filter(rows) > File "/home/jdemoor/programs/km/lib/python2.6/site-packages/ > sqlalchemy/util.py", line 1193, in unique_list > return [x for x in seq if x not in seen and not seen.add(x)] > TypeError: unhashable type: 'list' > > Full code : > > import os > from sqlalchemy import create_engine, Table, Integer, MetaData, Column > from sqlalchemy.orm import create_session, mapper > > sa_engine = create_engine(os.environ['TEST_DSN']) > session = create_session(sa_engine, autoflush=True, > expire_on_commit=True, autocommit=False) > > metadata = MetaData() > foo = Table('foo', metadata, Column('bar', Integer, primary_key=True)) > class Foo(object): > pass > mapper(Foo, foo) > > # This works > assert session.query('col').from_statement("SELECT 'abc' AS > col;").first() == ('abc',) > assert session.query('col').from_statement("SELECT > '{1,2,3}'::integer[] AS col;").first() == ([1,2,3],) > assert session.query('col1', 'col2').from_statement("SELECT > '{1,2,3}'::integer[] AS col1, 'abc' AS col2;").first() == ([1,2,3], > 'abc') > foo_obj = session.query(Foo).from_statement("SELECT 1 AS > foo_bar;").first() > assert foo_obj.bar == 1 > > try: > # This fails > session.query(Foo, 'col').from_statement("SELECT 55 AS foo_bar, > '{1,2,3}'::integer[] AS col;").first() > except TypeError, e: > print e > > from sqlalchemy.dialects.postgresql.base import ARRAY > col = Column('col', ARRAY(Integer, mutable=False)) > try: > # This fails too > session.query(Foo, col).from_statement("SELECT 55 AS foo_bar, > '{1,2,3}'::integer[] AS col;").first() > except TypeError, e: > print e > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
