简体   繁体   中英

Many to Many relation in prisma

Hello currently learning prisma and working with it realtion. I am trying to create many to many relation with students and classes. Each student can have many classes and each class can have many students. I am trying to test the api within nextjs api folder with folder student and add.js file. Upon trying to console result, I'm getting some error: Invalid `prisma.student.create(), class got invalid value. I am tryin to insert classes in my student, here's what I have.

prisma model:

model Student {
id             Int     @default(autoincrement()) @id
 firstName      String  
  lastName       String
  classes        CourseEnrollment[]
}

model Class {
  id             Int     @default(autoincrement()) @id
  name           String
  courseDetails  String?     
  members        CourseEnrollment[] 
}

model CourseEnrollment {
 studentId      Int
  student        Student  @relation(fields: [studentId], references: [id])
 classId        Int
 class          Class   @relation(fields: [classId], references: [id])
  @@id([studentId, classId])
}

add.js endpoint:

const result = await prisma.student.create({
   data: {
    firstName: 'John',
 lastName: 'doe',
  classes: { create: { class: "Math" } },
     },
    })
 console.log(result);
enter code here
const result = await prisma.student.create({
  data: {
    firstName: 'John',
    lastName: 'doe',
    classes: {
      create: [
        {
          class: {create: {name: "Math"}}
        }
      ]
    },
  },
})

console.log(result);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM