Skip to content
Community forum

Modelling a many-to-many relation (students and courses)

SolvedSupero SupportSupero team asked

Common question, written up by the Supero team.

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?

Link

1 answer

Accepted answer
Accepted answer

Don't add uuid fields to express a relationship. Declare it in references[] on one side with cardinality many and a reverse name:

json
"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:

bash
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.

Supero Platform teamSupero team answered
Link

Your answer

Modelling a many-to-many relation (students and courses) | Supero Community