hello, I try to use updateMany query method and I ...
# prisma-client
n
hello, I try to use updateMany query method and I want to update as well the relation.
Copy code
await this.prisma.test.updateMany({
      where: {
        status: 'active',
        endsAt: {
          lt: new Date(),
        },
      },
      data: {
        status: 'expired',
        and here the relation...
      },
    });
n
Hey @nikos l 👋, Currently, it’s not possible to update relations in an 
updateMany
. There’s a request here that you can follow that tracks this. Can you please leave a 👍 to the issue? It helps our engineering and product team in prioritising it. For your use case, I would suggest using a findMany to get the filtered records and then using
update
in a
$transaction
to update records with relations. Here is a reference for $transactions and an example of updating connected relations in update query
Copy code
const tests = await this.prisma.test.findMany({
  where: {
    status: 'active',
    endsAt: {
      lt: new Date(),
    },
  },
});

await prisma.$transaction(
  tests.map(test =>
    this.prisma.test.update({
      where: { id: test.id },
      data: {
        status:'expired',
        relation:{}
      },
    })
  )
)
n
thanks a lot @Nurul, yeah this is what I finally did, without the transaction going to add it. added the 👍 as well to the Github issue 🙂
🙌 1
👍 1