Search code examples
database-designschemaplanetscale

Many to many relationship or one to many with a separate pivot table?


Table clients can have many users. 99.9% of users belong to only one client. But there is a special type of user (auditors) that can “belong” to multiple clients.

Should I model this as a many to many relationship via a pivot table between the clients and users tables, or a one to many relationship and have a separate pivot table that tracks the many to many relationship just for this special auditor case?

I'm using PlanetScale (MySQL database) and Prisma as my ORM.


Solution

  • Have a 1:m relationship and then an intersection (not pivot) table to model the auditor relationship.

    Assuming that auditors exist in the user table, it may not be possible to limit just auditors being linked to the m:m table - using a pure modelling/SQL approach. You may need to implement this in your application logic