Using enum flags to model user roles in C#, SQL, and Go
Using an enum flags property to hold users’ roles is just one trick I’ve learned from @SeiginoRaikou, one of my co-geniuses at InterWorks.
Historically I have always modeled user roles with a Role table
and a many-to-many relationship with my User table.
+-------------------+
| User |
|-------------------|
| UserId : int +-------------+---------------+
| UserName : varchar| | UserRole |
+-------------------+ |---------------|
+ UserId : int |
+-------------------+ + RoleId : int |
| Role | |---------------+
|-------------------| |
| RoleId : int +-------------+
| RoleName : varchar|
+-------------------+
Compared to a flags enum approach this is downright wasteful. With flags, there is only ever one column to work with and it is an integer. No second table. No joins or second query.