I am using Prisma 2 and am having trouble connecting existing records on a model that has an implicit many to many relationship with the connected model.
model Event {
id Int @id @default(autoincrement())
performers Performer[]
}
model Performer {
id Int @id @default(autoincrement())
events Event[]
}
After running prisma migrations, I can see in postgres that I do have a join table created named _EventToPerformer
. I have created some performers in my database, and when I try to create an event and connect those existing performers, I am not able to.
const performers = await prisma.performer.findMany()
const event = await prisma.event.create({
data: {
performers: performers.map((p) => ({ id: p.id })),
},
include: {
performers: true,
},
});
For the above code, I am getting fairly cryptic typescript errors on the data
property of the object I am passing in to the create function. When I suppress with @ts-ignore
, I getting these prisma client errors:
Unknown arg `performers` in data.performers for type EventUncheckedCreateInput. Available args:
type EventUncheckedCreateInput {
id?: Int
}
Is there anything obvious I am missing when setting up an implicit many to many relationship, and/or attaching existing records to these models?
You are missing a connect
. So you're code should look like:
const performers = await prisma.performer.findMany()
const event = await prisma.event.create({
data: {
performers: {
connect: performers.map((p) => ({ id: p.id })),
}
},
include: {
performers: true,
},
});
You can read more about working with relations here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.