🧹

postgresqlで値の組み合わせ一致で複数削除する

2024/03/13に公開

以下

delete from mytable where (id1, id2) = ANY(Array [(1,3), (2,1)]);

https://stackoverflow.com/questions/75341609/batch-delete-rows-matching-multiple-columns-in-postgres-using-an-array

prismaなら以下

const arrSql = Prisma.join(
  targets.map(target => Prisma.sql`(${Prisma.join([target.type, target.id])})`)
);

await prisma.$executeRaw`DELETE FROM my_table WHERE some_key = ${someKey} AND (target_type, target_id) = ANY(ARRAY [${arrSql}])`;

Discussion