I am trying to create User, Group and Membership tables as follows. A User may belong to one or many groups and a Group may have one or many users. The many-to-many relationship between User and Group table is maintained using Membership table. I would like to maintain Membership status (approved/pending/something-else??) and Membership approver information as well. The membership approver will be one of the user in the admin group. Following is a rough schema that I have in my mind. I am new to database modeling and would like to get some feedback on following implementation. In particular I am not sure whether Membership table should have ‘approved_by’ column using ‘user_id’ FK or should I create a separate table for maintaining MembershipStatus(id, membership_id, status, approved_by)?? I am guessing having a separate table MembershipStatus is more appropriate here but not sure about it. Also, I am planning to use ActiveRecord as an ORM in this application. I appreciate any comments or suggestions in implementing this design/schema.
User:
id, Primary Key
userid, Integer, Unique
username, String, Unique
email, String, Unique
created_at, DateTime
updated_at, DateTime
Group:
id, Primary Key
groupid, Integer, Unique
groupname, String, Unique
created_at, DateTime
updated_at, DateTime
Membership:
PK(user_id, group_id)
status, String
approved_by, user_id FK to User table
created_at, DateTime
updated_at, DateTime
MembershipStatus
id
membership_id FK to Membership table
status, String
approved_by, user_id FK to User table
created_at, DateTime
updated_at, DateTime
-thanks
looks pretty good.
You show approved_by in 2 tables. If you keep the MembershipStatus table, then that is where is should belong. also -the name ‘approved_by’ implies the status of ‘approved’ which may or may not exist. you might think of another name…
you should also have a table probably that identified which Users are admins for which groups. that way you can code up database side security for who is allowed to approve as well as store who actually did approve.
also, I am not a big fan of storing audit history in the table as you are showing. Either use the database built in audit, or pull that out to another table to record audit history.
finally, id, and userid seem redundant. go with userid. (similar on other tables)