postgres/psycopg:无法将多个命令插入准备好的语句
我有两个postgres数据库表,它们之间有关系,但没有设置级联删除。所以我需要手动从相关的表和主表中删除数据。
我正在使用sqlalchemy核心库和psycopg3驱动。
我有以下代码:
identifiers = ['id100', 'id200']
sql = """
delete from related_table where entity_id in (select f.id from main_table f where identifier in (%(id1)s));
delete from main_table where identifier in (%(id2)s)"""
params = {'id1': identifiers, 'id2': identifiers}
conn = self.db_engine.raw_connection()
cursor = conn.cursor()
cursor.execute(text(sql), params=params)
这段代码导致了以下错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.11/site-packages/psycopg/cursor.py", line 732, in execute
raise ex.with_traceback(None)
psycopg.errors.SyntaxError: cannot insert multiple commands into a prepared statement
那么,怎样才能正确地执行多个语句,而不需要循环(这样就可以避免多次往返请求)呢?
0 个回答
暂无回答