Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2: (col1, col2) IN my_list: ProgrammingError: syntax error at or near "ARRAY"

I want to execute this sql via psyopg2:

select indexname from pg_indexes where (tablename, indexname) in ( 
      ('tab1', 'index1'),
      ('tab2', 'index2') 
);

Here is the code:

cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
    [('tab1', 'col1'), ('tab2', 'col2')],
               ])

I get this exception:

ProgrammingError: syntax error at or near "ARRAY"
LINE 1: ...e from pg_indexes where (tablename, indexname) IN ARRAY[('ta...

How to pass a list of tuples to PostgreSQL vis psyopg2?

like image 401
guettli Avatar asked Oct 29 '15 09:10

guettli


1 Answers

If you pass a tuple instead a list, it works:

cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
    tuple([('tab1', 'col1'), ('tab2', 'col2')]),
               ])

Don't ask my why it fails if you pass a list.

like image 107
guettli Avatar answered Oct 11 '22 22:10

guettli