Като увеличите малко усилията си, какво ще кажете за:
quote_1 = ("monkeys rock", "False")
quote_2 = ("donkeys rock", "True")
q_list = [ quote_1, quote_2]
print cur.mogrify("insert into posts VALUES(%s,%s::quotes[])",
("animals are good", q_list))
#
# added explicit cast to quotes[]->^^^^^^^^
Обяснение :
Ако стартирате:
insert into posts
VALUES('animals are good', ARRAY[
('monkeys rock', 'false'),
('donkeys rock', 'true')
]);
директно в psql
ще получите:
regress=# insert into posts
regress-# VALUES('animals are good',ARRAY[
regress-# ('monkeys rock', 'false'),
regress-# ('donkeys rock', 'true')
regress-# ]);
ERROR: column "q" is of type quotes[] but expression is of type record[]
LINE 1: insert into posts VALUES('animals are good',ARRAY[('monkeys ...
^
HINT: You will need to rewrite or cast the expression.
Със сигурност казвате на Pg, че вашият анонимен масив е от тип quotes[]
върши работа:
regress=# insert into posts
regress-# VALUES('animals are good',ARRAY[
regress-# ('monkeys rock', 'false'),
regress-# ('donkeys rock', 'true')
regress-# ]::quotes[]);
INSERT 0 1
regress=# select * from posts;
body | q
------------------+--------------------------------------------------------
animals are good | {"(\"monkeys rock\",false)","(\"donkeys rock\",true)"}
(1 row)