CreateMany not allowing me to create nested M:N entities

I've got a situation where this query works:
await prisma.item.create({
data: {
order: 1,
sectionId: "1",
itemTags: {
create: [
{
tagId: "1",
},
],
},
},
});
await prisma.item.create({
data: {
order: 1,
sectionId: "1",
itemTags: {
create: [
{
tagId: "1",
},
],
},
},
});
But this doesn't:
await prisma.item.createMany({
data: [
{
order: 1,
sectionId: "1",
itemTags: {
create: [
{
tagId: "1",
},
],
},
},
],
});
await prisma.item.createMany({
data: [
{
order: 1,
sectionId: "1",
itemTags: {
create: [
{
tagId: "1",
},
],
},
},
],
});
Right now my prisma schema looks sort of like this:
model Tag {
...stuff
itemTags ItemTag[]
}
model ItemTag {
id String @default(cuid())
itemId String
tagId String
item Item @relation(fields: [itemId], references: [id])
tag Tag @relation(fields: [tagId], references: [id])

@@id([itemId, tagId])
}
model Item {
...stuff
itemTags ItemTag[]
}
model Tag {
...stuff
itemTags ItemTag[]
}
model ItemTag {
id String @default(cuid())
itemId String
tagId String
item Item @relation(fields: [itemId], references: [id])
tag Tag @relation(fields: [tagId], references: [id])

@@id([itemId, tagId])
}
model Item {
...stuff
itemTags ItemTag[]
}
3 Replies
Jon Higger (He / Him)
I am currently using prisma with postgres
jonfanz
jonfanz2w ago
If I’m understanding correctly, you’re trying to create many items and also one or more item tags for those items. That currently isn’t possible with Prisma: https://www.prisma.io/docs/orm/reference/prisma-client-reference#createmany (see under remarks) A workaround is to create many items and then make a second call (perhaps in a transaction) to create tags. Similar to this example: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#using-nested-createmany
Relation queries (Concepts) | Prisma Documentation
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Prisma Client API | Prisma Documentation
API reference documentation for Prisma Client.
Jon Higger (He / Him)
Dang that’s going to be a surprisingly painful refactor for me, right now this is a small piece of the puzzle in a large deep clone So restaurants have menus have section groups have sections have items But items have a many to many with tags 😂 I’ve got a strategy but it’s going to be pretty 🤮
Want results from more Discord servers?
Add your server