Hi, The way to add join records to a many to many...
# orm-help
s
Hi, The way to add join records to a many to many is like this...
Copy code
await prisma.role.update({
    where: { id: 1 },
    data: {
      users: {
        create: [
            { user: { connect: { id: 1 } } },
            { user: { connect: { id: 2 } } },
            { user: { connect: { id: 3 } } },
        ]
      }
    }
  })
How would I remove records from a many to many? I can do it like this...
Copy code
await prisma.role.update({
      where: { id: 1 },
      data: {
        users: {
          deleteMany: [
           { user_id: 1 },
           { user_id: 2 },
           { user_id: 3 },
         ]
        }
      }
    })
But is there a similar way of doing it like the first example just with 
disconnect
?
m
Your last example will actually delete those users, not disconnect. Is that what you're looking for:
Copy code
await prisma.implementationRole.update({
      where: { id: 1 },
      data: {
        users: {
          disconnect: [{ id: 1 }, { id: 2 }, { id: 3],
    },
  },
})
Your first example is also a little strange. Can you explain more what do you want to achieve?
s
I've tried your example but it doesn't work. I'm getting errors that there is no
users.0.user.id
That case is similar to what the docs mention. I think my case is different since it's a many to many with a join table. Your case works for a one to many like user to posts. In my first example. I have a
roles
table with a
users
table and then a join table of
user_roles
A
User
as
roles
and a
Role
has
users
. I my first example I'm creating
user_role
records by
connecting
an existing
user
to a
role
. In my 2nd example I would like to
delete
user_role
records by
disconnecting
the existing
user
. I don't know if this is possible, but it sounds like the inverse of my first example, which is why I thought it should be feasible.
m
I see what you mean but can you post your schema, so it would be easier to see this?
s
Sorry I didn't see your question until now. Here's my schema. Thanks.
Copy code
model User {
  id         Int    @id @default(autoincrement())
  first_name String @db.VarChar(50)
  last_name  String @db.VarChar(50)

  roles      UserRoles[]

  @@map("users")
}

model Role {
  id    Int         @id @default(autoincrement())
  name  String      @unique @db.VarChar(50)
  users UserRoles[]

  @@map("roles")
}

model UserRoles {
  user    User @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
  user_id Int
  role    Role @relation(fields: [role_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
  role_id Int

  @@id([user_id, role_id])
  @@map("user_roles")
}
m
I don't think it will work since
disconnect
is only for relations which are optional. Those records in UserRoles should be deleted anyway, since join table has both relations required. So either use your nested
deleteMany
or just like this:
Copy code
await prisma.userroles.deleteMany({
 where: {
  userId: { in: [1,2,3] },
  roleId: 1
  }
});
s
Thank you!