Looking for advice on how to manage updating relat...
# orm-help
c
Looking for advice on how to manage updating relations. I'd like to be able to update relations in one operation, i.e. updating the categories of a product. Currently in the docs I see how to connect or disconnect relations, here is the example in the docs:
Copy code
const getUser = await prisma.user.update({
  where: {
    id: 9
  },
  data: {
    posts: {
      connect: {
        id: 11
      },
      create: {
        title: "My new post title"
      }
    }
  }
})
Unfortunately, I don't believe this will disconnect previously related posts i.e. a post with an
id
of
5
that should no longer be related to this user. This means that (in this example) when the API receives an array of post IDs that should be related to this user, I first have to query which posts are currently related to the user, then run another query to connect new posts, and yet another query to disconnect old posts. Think about a situation where on the front end, someone can edit a product and assign it to zero or more categories via a multi-select. Same problem. Seems unfortunate there's no way to update a many-to-many relation in one operation, or maybe I've missed it? I know you can connect an array of IDs, but again I'm guessing that won't disconnect IDs that aren't in the array, or all IDs if the array is empty.
1
c
Excellent thank you 🙌
👍 1