In my Prisma schema, I have a many-to-many relationship between posts and categories. I've added @map
options to match the Postgres snake_case naming convention:
model Post {
id Int @id @default(autoincrement())
title String
body String?
categories PostCategory[]
@@map("post")
}
model Category {
id Int @id @default(autoincrement())
name String
posts PostCategory[]
@@map("category")
}
model PostCategory {
categoryId Int @map("category_id")
postId Int @map("post_id")
category Category @relation(fields: [categoryId], references: [id])
post Post @relation(fields: [postId], references: [id])
@@id([categoryId, postId])
@@map("post_category")
}
I'm trying to create a post with multiple categories at the same time. If a category exists, I'd like to connect
the category to the post. If the category doesn't exist, I'd like to create it. The creation part is working well, but the connection part is problematic:
await prisma.post.create({
data: {
title: 'Hello',
categories: {
create: [{ category: { create: { name: 'News' } } }],
connect: {
categoryId_postId: { categoryId: 1, postId: ? }, // This doesn't work, even if I had the postId
},
},
},
});
How can I connect an existing category to a new post with the schema that I have?
{ categoryId: 1, postId: 1 }
bridge record exists in thepost_category
table, then it replaces the bridge record with{ categoryId: 1, postId: new_id }
. In other words, it re-assigns another post's category to this new post. I don't want any other post to be impacted when I create a new post. I'd like to create a new category if the category doesn't exist, or add a new bridge record if it does. There's a good example usingcreate
andset
on implicit relations here: (url to follow), but it doesn't work b/c I'm using explicit relation. – Natalianatalie