Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

meetgodhani's avatar

Database Schema For Users belonging to Groups with Roles

Hi,

What is the best way to design database schema for Users belonging to multiple Organization/Group and their roles in that Group.

For example,

Sam is part of group PHP and he is also owner of the group

Joe is part of group PHP and his role is member.

Also transferring ownership of the group/organization if he or she doesn't belong to that organization/group anymore and is also creator/owner.

So how do we design that kind of schema ?

0 likes
5 replies
bobbybouwmann's avatar
Level 88

The best way would be a pivot table with an extra field

users
- id 
- email
- password  

groups
- id
- name

group_user
- group_id
- user_id
- role (owner, creator and so on)

If you want multiple roles for a group_user you will need an extra pivot table

1 like
bobbybouwmann's avatar

You can have an extra table for roles, however that means that you need to connect the user to the that role and the user_group table as well. That means an extra table.

1 like
meetgodhani's avatar

@bobbybouwmann If we go adding extra attribute in pivot table how would I query just to retrieve organization where user is admin.

bobbybouwmann's avatar

Just add the keyword in there. So you would have a record like so

group_user

group_id => 1
user_id => 1
role => "admin"

group_id => 1
user_id => 2
role => "member"

Please or to participate in this conversation.