Many to many relationship with extra fields

Hello, I’m new to Strapi and I’m trying to understand how to add additional attributes to a many to many relationship. I’ve seen few threads regarding this but I’m not sure I could find a conclusive answer.

This is my scenario:

  • I have a User and a Course content type
  • I want to create a many to many relationship between the two but also store in the relationship extra attributes to track if the user is enrolled or has completed the courses.

If I had to do this directly on the DB side I would have a Users table, Courses table and an User Courses intersection table referencing Users and Courses and having the extra attributes.

What I have done in Strapi is creating a User_Course content type with the extra attributes I need and then add a one to many relationship from User to User Course and a many to one relationship from Course to User_Course.

This kind of work but in the back-end it results in to additional link tables that strictly speaking wouldn’t be necessary to capture the relationship and add complexity to data retrieval.

Is there any other way to achieve this?

Andrea

1 Like

Hello @Andrea_Cardinale,

From what I understand, you don’t want to create a 3rd collection type just to keep track of the progress, but it is impossible to be achieved this without storing it somewhere.

I would recommend structuring it like this

Collection Types:

  • User (relations: many-to-many with Courses, one-to-many with User Progress)
  • Course (relations: many-to-many with Courses)
  • User Progress (relation: has one course, has one user)

Lifecycles:

  • When you create a relation between User and Course, you should also create the User Progress, and attach the User and Course to User Progress.
  • When the user completes the course, you should modify the User Progress and change the status from enrolled to completed.
  • When you fetch the User with courses, you should also fetch the data from User Progress, by filtering User & Course)

Hello, I’m having the same conundrum. In the above example the place I would like to store the data is on the User_Course_Links table rather than having to have another User Progress object. I worry about the extra overhead this causes in complexity. It would feel more right to have the data architecture as small and elegant as possible.

Would it be possible to write a plugin to enable this sort of structuring?