medihack
medihack

Reputation: 16627

Re-create related records on update

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)?

Upvotes: 0

Views: 50

Answers (1)

Ionuț Păduraru
Ionuț Păduraru

Reputation: 339

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.

Upvotes: 1

Related Questions