type StudentCourses
@model(queries: null)
@auth(rules: [{ allow: public }])
@key(name: "byStudent", fields: ["studentID", "courseID"])
@key(name: "byCourse", fields: ["courseID", "studentID"]) {
id: ID!
studentID: ID!
courseID: ID!
student: Student! @connection(fields: ["studentID"])
course: Course! @connection(fields: ["courseID"])
}
type Course
@model
@auth(rules: [{ allow: public }])
@key(name: "bySchool", fields: ["schoolID"]) {
id: ID!
courseName: String!
schoolID: ID!
students: [Student!] @connection(keyName: "byCourse", fields: ["id"])
}
type Student
@model
@auth(rules: [{ allow: public }])
@key(name: "bySchool", fields: ["schoolID"]) {
id: ID!
studentName: String
studentEmail: String
sisID: Int
schoolID: ID!
courses: [Course!] @connection(keyName: "byStudent", fields: ["id"])
}
I followed the steps in docs to create a many to many relationship between student and courses but i keep getting this error Key byCourse does not exist for model Student even thought i created a model to connect school and courses. Thanks in advance
You need to omit keyName
from Course
, since in Student
model, you have list of courses, so you can't really have @key
directive for that.
https://docs.amplify.aws/cli-legacy/graphql-transformer/connection/#many-to-many-connections
type Course
@model
@auth(rules: [{ allow: public }])
@key(name: "bySchool", fields: ["schoolID"]) {
id: ID!
courseName: String!
schoolID: ID!
students: [Student!] @connection(fields: ["id"])
}