I want to use Prisma to associate articles with tags, that is: A post has multiple tags and a tag belongs to multiple posts
But the example in Prisma will result in the creation of duplicate tags
Here is my Prisma model
model Article {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String
summary String
link String @unique
archive String
content String
image String
tags Tag[]
}
model Tag {
id Int @id @default(autoincrement())
name String
articles Article[]
}
This is my code according to the documentation, it causes duplicate tags to appear
await prisma.article.create({
data: {
title,
summary,
link,
archive,
content,
image,
tags: {
create: tags.map((tag) => ({ name: tag })),
},
},
});
When I use connectOrCreate, it reports an error in many-to-many mode
await prisma.article.create({
data: {
title,
summary,
link,
archive,
content,
image,
tags: {
connectOrCreate: {
where: tags.map((tag) => ({ name: tag })),
create: tags.map((tag) => ({ name: tag })),
},
},
},
});
tags.map((tag) => ({ name: tag }))
within your code ? – Bodgie