我目前在postgres中有一个自定义类型:
create type times_type as
(
start varchar,
end varchar,
rate real
);
我也有一个函数,它需要一个times_type数组:
create function insert_data(times_values times_type[])
当我使用命名元组并使用callproc一次插入一个值时,它运行良好,即:
Document = namedtuple('times_type', 'start
end rate')
cur.callproc('insert_data',
(Document('13:00', '16:00', 12.56))
但是现在函数正在期待一个数组,所以我尝试了:
Document = namedtuple('times_type', 'start
end rate')
cur.callproc('insert_data',
([Document('13:00', '16:00', 12.56),
Document('17:00', '18:00', 12.56),
Document('19:00', '20:00', 12.56)])
psycopg2给我一个错误,该函数不存在。处理自定义类型数组时,您需要做些什么吗?
复合类型数组的问题在于必须显式地转换它们,否则Postgres会将其视为record[]
。使用register_adapter(class, adapter)
。
Document = namedtuple('times_type', 'start end rate')
class DocumentAdapter:
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() + b'::times_type'
psycopg2.extensions.register_adapter(Document, DocumentAdapter)
cur.callproc('insert_data',
([Document('13:00', '16:00', 12.56),
Document('17:00', '18:00', 12.56),
Document('19:00', '20:00', 12.56)],)) # comma added, this should be a tuple
conn.commit(); # dont forget to commit
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句