This project is archived and is in readonly mode.
I have bug with array of composite types
-
Daniele Varrazzo
- State changed from new to invalid
- Tag changed from compositetype, rel-2.5.3 to compositetype, question, rel-2.5.3
Try adding an explicit cast after your placeholder specifying that the type you are passing is an array of
test_comp_type. This should work:cur.execute("INSERT INTO test_comp_table VALUES (%(data)s::test_comp_type[])", dict(data=[("first", "second")])) -
Nikita Kuznetsov
No, I want autocast, because I use sqlalchemy and I work througth sqlalchemy interface.
-
Daniele Varrazzo
In this case you should create your data as a specific python type, it cannot be a generic tuple, and register a typecaster for it. For instance:
Comp = namedtuple('Comp', 'first second') class CompAdapter: def __init__(self, x): self.adapted = psycopg2.extensions.SQL_IN(x) def prepare(self, conn): self.adapted.prepare(conn) def getquoted(self): return self.adapted.getquoted() + '::test_comp_type' psycopg2.extensions.register_adapter(Comp, CompAdapter) # this now works cur.execute("INSERT INTO test_comp_table VALUES (%(data)s)", {'data': [ Comp('a', 'b'), Comp('c', 'd') ]})