You can refer to the five tables here: http://www.lyblog.net/detail/...
Actually there is a table:
There is a one-to-one correspondence between users and roles. Why not just add a role field to the user table instead of creating another table?
What are the benefits of doing this?
Supplement:
Some people say that users and roles have a one-to-many relationship. I want to know why it is necessary to set it to one-to-many?
Isn’t it easier to handle a one-to-one relationship?
You can refer to the five tables here: http://www.lyblog.net/detail/...
Actually there is a table:
There is a one-to-one correspondence between users and roles. Why not just add a role field to the user table instead of creating another table?
What are the benefits of doing this?
Supplement:
Some people say that users and roles have a one-to-many relationship. I want to know why it is necessary to set it to one-to-many?
Isn’t it easier to handle a one-to-one relationship?
My understanding is that a user can have multiple roles
It’s like this:
A person can have multiple roles
A role can also be used by multiple people
Many-to-many relationships generally require an intermediate table to be mapped.
Unless you are using a non-relational database like redis
Not one-to-many, but many-to-many.
Maybe your project doesn’t need it, but as a framework, you need to consider broader needs. From this perspective, a many-to-many table can be used as a one-to-one table, because many-to-many includes one-to-one, but one-to-one A one-to-one design will inevitably fail to meet the many-to-many needs. So there's going to be a separate relationship table here, got it?
A user has multiple roles.
In fact, it is possible not to create a separate table.
Decoupling, single responsibility model
The relationship of ManytoMany requires an intermediate table!
In order to meet the many-to-many needs
My understanding may not be accurate: For example, there are several roles a, b, and c. According to your understanding, users 1, 2, and 3 can correspond to a, b, and c respectively. The character is okay. If another user 4 comes, the role he needs happens to have the permissions of roles a and b, so user 4 directly belongs to roles a and b. There is no need to create a new role d to meet the needs of user 4