3

Following is my table structure in postgresql:

CREATE TYPE lang AS ENUM ('english','hindi','marathi');
CREATE TABLE assignment (
    title varchar(100),
    ids lang[]
);

I want to insert a record in assignment table using parameter binding in python. I tried below code:

cursor.execute('insert into assignment (title, ids) values (%s, %s)',["person1",["english","hindi"]])

But it generates invalid query like:

insert into assignment (title, ids) values ('person1', ARRAY['english','hindi'])

And hence I get syntax error in query.

Correct query is:

insert into assignment (title, ids) values ('person1', '{"english","hindi"}')

Can someone let me know way to generate correct query using parameter binding?

2
  • That is valid array syntax. What is the error message? Commented Apr 3, 2017 at 19:02
  • ERROR: column "ids" is of type lang[] but expression is of type text[] Commented Apr 3, 2017 at 19:13

1 Answer 1

4

Cast the passed array to lang[]:

cursor.execute('''
    insert into assignment (title, ids) 
    values (%s, %s::lang[])
    ''', ["person1",["english","hindi"]])
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.