Hi guys, I want to update a record and update to n...
# orm-help
r
Hi guys, I want to update a record and update to new related records in a single call. I saw that we can use
set: []
to disconnect all related records. Will this work ?
Copy code
prisma.user.create({ 
data: { 
  name: 'test', 
  likes: { 
   set: [], 
   connect: [{ name: 'a'}, {name: 'b'}]
  }
});
βœ… 1
r
Hi @Rain πŸ‘‹ To update and disconnect all related related records, You could do something like:
Copy code
const updateUser = await prisma.user.update({
  where: {
    id: 16
  },
  data: {
    posts: {
      set: []
    }
  },
  include: {
    posts: true
  }
})
This results in the output
Copy code
{
  id: 16,
  name: null,
  email: '<mailto:orla@prisma.io|orla@prisma.io>',
  profileViews: 0,
  role: 'USER',
  coinflips: [],
  posts: []
}
Check out the docs for more information on this.
r
I want to actually update the record including the related records from value passed by user. So to do that what I am thinking basically reset the related record and reconnect with new value.
Okay confirmed it work. using set:[] to disconnect and connect to make it in single call love it ☺️☺️
r
I'm glad to know you love it.
r
i hope the set will always take priority when executed else it will connect and disconnect all πŸ˜‚