For the one many-to-many relationship I have, I just decided to let Prisma implicitly create the join table:
An article can have many tweets (if some tweets linked an article in it) and a Tweet can have many articles( if multiple ones mentioned).
model Article {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
publishedAt DateTime @default(now())
entry Entry @relation(fields: [entryId], references: [id])
entryId String @unique
author String
siteName String?
title String
...
tweets Tweet[]
}
model Tweet {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
publishedAt DateTime
entry Entry @relation(fields: [entryId], references: [id])
entryId String @unique
author String
...
articles Article[]
}
And they are not required because sometimes tweets don’t have articles and vice versa.
Prisma creates the join table with A and B … literally A and B:
But works fine.
You can then link existing via update
and connect
:
logger.debug(
{ articleId: article.id, article: article },
`Linking article ${article.id} to tweet ${tweet.id}`
)
await db.tweet.update({
where: { id: tweet.id },
data: { articles: { connect: [{ id: article.id }] } },
})