armorform
armorform
PPrisma
Created by armorform on 4/24/2024 in #help-and-questions
Many to many on the same model, multi directional?
What I'm doing now is basically returning an object that is an array of both include fields:
relatedArtists: [...artist.relatedArtists, ...artist.artists].filter(
(n) => n,
),
relatedArtists: [...artist.relatedArtists, ...artist.artists].filter(
(n) => n,
),
And as you can imagine, it's kind of annoying / messy. What I'd like to do is quite simple; just have the same "include" field returned on both models so I don't have to do this messy back and forth juggling between which "side" the relation was originally created on. Anyways, thanks in advance SO much for your help! I promise I've already spent a few hours trying to wrap my head around this...
5 replies
PPrisma
Created by armorform on 4/24/2024 in #help-and-questions
Many to many on the same model, multi directional?
^ here's what the schema looks like, and I think what I'm better getting at is how to avoid having to do this:
relatedArtists: {
include: {
artist: true,
relatedArtist: true,
},
},
relatedArtists: {
include: {
artist: true,
relatedArtist: true,
},
},
5 replies
PPrisma
Created by armorform on 4/24/2024 in #help-and-questions
Many to many on the same model, multi directional?
model Artist {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

slug String @unique

relatedArtists RelatedArtist[] @relation("relatedArtist")
artists RelatedArtist[] @relation("artists")
}

model RelatedArtist {
relatedArtistId String
relatedArtist Artist @relation("relatedArtist", fields: [relatedArtistId], references: [id], onDelete: Cascade, onUpdate: Cascade)

artistId String
artist Artist @relation("artists", fields: [artistId], references: [id], onDelete: Cascade, onUpdate: Cascade)

@@id([artistId, relatedArtistId])
}
model Artist {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

slug String @unique

relatedArtists RelatedArtist[] @relation("relatedArtist")
artists RelatedArtist[] @relation("artists")
}

model RelatedArtist {
relatedArtistId String
relatedArtist Artist @relation("relatedArtist", fields: [relatedArtistId], references: [id], onDelete: Cascade, onUpdate: Cascade)

artistId String
artist Artist @relation("artists", fields: [artistId], references: [id], onDelete: Cascade, onUpdate: Cascade)

@@id([artistId, relatedArtistId])
}
5 replies