Re-create related records on update

Solution for Re-create related records on update
is Given Below:

I want to re-create some related records on update, something like this:

db.post.update({
  where: { id: 1 },
  data: {
    title: "The first post",
    comments: {
      set: [{ content: "foo"}, { content: "bar" }],
    },
  },
})

This is unfortunately not possible as set only allows to reference existing comments (by using the id). So is there another way during an update to delete all those related comments and re-create them from scratch in one transaction (and maybe on nested write)?

I assume you are referring to Prisma ORM.

In order to re-create related records (i.e. delete previous related records and add new ones) you will need to use deleteMany along with create, something like this:

db.post.update({
  where: { id: 1 },
  data: {
    title: "The first post",
    comments: {
      // delete existing relations
      'deleteMany': {},
      // create new relations
      'create': [
         ......your new data here....
      ],
    },
  },
})

See this comment on github for an update example.