Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma delete many to many relationship with Composite Key

I have this schema here:

model label {
  title         String          @id @db.VarChar(16)
  color         String          @db.VarChar(16)
  labelplaylist labelplaylist[]
}

model labelplaylist {
  playlistId Int
  labelId    String   @db.VarChar(16)
  label      label    @relation(fields: [labelId], references: [title])
  playlist   playlist @relation(fields: [playlistId], references: [id])

  @@id([playlistId, labelId])
  @@index([labelId], name: "labelId")
}

model playlist {
  id              Int             @id @default(autoincrement())
  createdAt       DateTime?       @default(now()) @db.DateTime(0)
  title           String          @db.VarChar(100)
  labelplaylist   labelplaylist[]

  @@index([userId], name: "userId")
}

And I would like to delete only the relation between the label and the playlist table. I tried it to do like this:

const deleteRelation = await prisma.labelplaylist.delete({
    where: {
        playlistId_labelId: 
    },
})

I have the primary key of the label and playlist table, but I don't know how I get the primary key => playlistId_labelId.

Thank's for helping out.

like image 838
Holunderyogele Avatar asked Oct 21 '25 04:10

Holunderyogele


1 Answers

Here's the syntax for queries with composite key

    const deleteRelation = await prisma.labelplaylist.delete({
        where: {
            playlistId_labelId: {
                playlistId: playListIdVariable, //replace with appropriate variable
                labelId: labelIdVariable, //replace with appropriate variable
            },
        },
    });

You can read more in the get record by compound ID or compound unique identifier subsection of the CRUD Reference Guide in the Prisma docs. This reference shows reading data, but the where condition is similar for delete and update as well.

like image 50
Tasin Ishmam Avatar answered Oct 22 '25 18:10

Tasin Ishmam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!