A student takes many courses and a course has many students. In Postgres we'd have an enrolment join table. Should the student schema get a course_uuids array field?
Modelling a many-to-many relation (students and courses)
Common question, written up by the Supero team.
1 answer
Don't add uuid fields to express a relationship. Declare it in references[] on one side with cardinality many and a reverse name:
"references": [
{"name": "Course", "cardinality": "many", "back_ref_name": "students"}
]The platform manages the link. Add or remove one edge with ref-update, or many at once with PUT /api/v1/crud/{domain}/ref-update-multi:
curl -X PUT https://api.supero.dev/api/v1/crud/acme/ref-update \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"type": "student", "uuid": "<student-uuid>", "ref-type": "course", "ref-uuid": "<course-uuid>", "operation": "ADD"}'Read a student's courses with GET /api/v1/crud/{domain}/student/{uuid}/refs/course. For the other direction, GET /api/v1/crud/{domain}/course/{uuid}/back-refs returns the records that point at that course, and /list-bulk takes back_ref_id for the same thing in a list call.
Adding a reference needs create-or-update permission on the source type and read on the target, so check the student role's policy if ADD is refused. If the enrolment carries its own data, such as a grade or a start date, make it a schema of its own with a one reference to Student and a one reference to Course.
Your answer
Know the answer? Sign in to post it. It takes an email address and a 6-digit code.
Sign in to answer