DB Normalization for Users and Roles with different attributes - database-normalization

What would be the correct way to normalize a scenario where we have Users and Roles where certain roles have the same attributes and other roles have different attributes? Let's take the following example:
tb_User(user_id, first_name, last_name, e_mail);
tb_Role(role_id, role)
tb_Plant(plant_id, plant)
tb_Entity(entity_id, entity)
I have roles like: Admin, General Manager and Plant Manager and Entity Manager. Admin and General Manager roles don't have any other attributes, but plant manager has a plant associated with it and Entity Manager has an entity associated with it. Should I create one table like this and have Plant/Entity have a value where it applies like this:
tb_user_role(user_role_id, user_id, role_id, plant_id, entity_id)
or have separate tables for plant and entity like this:
tb_user_role(user_role_id, user_id, role_id)
tb_user_role_entity(user_role_entity_id, user_role_id, entity_id)
tb_user_role_plant(user_role_plant_id, user_role_id, plant_id)
Another consideration is that one one user can have many plants, so one plant manager role and many plants, same thing for entity.
I appreciate your help,

Related

Strapi add extra field in the relationship table

I am using Strapi for my application. I am using Postgres as a database. There is user table. I want to create a Teams table linked with Team Leaders and Team Members related to users table. I added team name, team code, and Team Leaders(Relationship field -> Many to Many Relationship), Team members(Relationship field -> Many to Many Relationship)
After I did this, My table fields look like this,
Teams,
Id, name, code
teams_team_leaders__users_team_leaders
Id, Team id, User id
teams_team_members__users_teams
Id, Team Id, User id
But here I wanted to add one extra field (custom_role) in the teams_team_leaders__users_team_leaders and teams_team_members__users_teams table. Is there any way to add a new field/column into Relationship tables?

What's the relationship between res.partner and res.user?

I am new to odoo v8 and i am not able to understand the relationship between res_partner and res_users tables and also with hr_employee table are they all related?
The relationship between res.partner and res.user is that res.user inherits from res.partner using an inheritance type called "Delegation Inheritance" (see documentation).
Because of "Delegation Inheritance" every res.user record has a mandatory internal connection to a corresponding res.partner record using a field partner_id. What is this connection all about is to directly use all the fields of res.partner to store data shared by res.user and res.partner (i.e. name, phone, etc... if for example you refer to phone property of a record of res.user you'll get the value stored in the corresponding res.partner record) so res.user has to define fewer number of fields on it's own, like password, login, etc..
Note also that because of this relation res.user can NOT exist in the system without corresponding res.partner, it's why every res.user has one, but nonetheless res.partner can exist without res.user.
hr.employee have m21 with res.users (user_id)
res.users have m21 with res.partner(partner_id)
Actually only res.users has a "real" relationship to res.partner, because with every user odoo will create a partner (per default no customer and no supplier). this partner will be used e.g. for emails and the followers system in odoo.
But you can have partners without users, too. That will be a normal partner, for defining customers and suppliers.
And finally there is the employee. You can set a user on it. If i recall right, the user will be used for attendances and timesheets.

How to relate entities - users with multiple roles

I have 3 entities to represent Users, Roles and Conferences
So far I got this diagram:
So,
-A user can be associated with zero or more conferences.
-A conference may have one or more users.
and...
-The same user can have different roles depending on which conference he is.
but...
-How can I improve the diagram so i can see the different roles of a user in all the conferences he has attended?
[UPDATE]
From your description it sounds like users belong to roles, and then the combination of userRoles belongs to a conference. So, without the fancy diagram, your entities would be something like this...
Users
Id
Roles
Id
Conferences
Id
UserRoles
UserId
RoleId
ConferenceUserRoles
ConferenceId
UserId
RoleId
You may need to add a "UserRoleId" to UserRoles and use that in ConferenceUserRoles. I'm not exactly sure how EF will handle the three-way relation table.
Hope this helps!

JPA - Join Two tables

I have two tables, namely
USER_ROLE {user_id, Role} PK {user_id, role}
ROLE_PERMISSION {role, permission} PK {role, permission}
A User can have multiple Roles.
A Role can be mapped to multiple
Permissions.
I have a entity - USER that maintains information about the User. This info is fetched via LDAP (not DB) on first login. Now, for my authorization aspects, I need to also fetch dtls on User's permissions from above mentioned tables.
So I would imagine adding attributes to my existing USER entity
USER {
user_id,
first_name,
last_name,
etc
// Authorization
List<String> roles;
List<String> permissions;
}
Can someone pls help how I can use JPA to populate the roles and permissions Lists? Looked over internet, can't figure it out. thanks
I would create a USER table in your database and map it to a User object with the role and permissions. The User object then would include additional LDAP data.
Without a USER table you have nothing to map to.
Otherwise just query for the database using native SQL queries and populate your LDAP user object yourself.

Entity Framework v1 Modelling Many-to-Many Lookup Table Relationship

I have the following database tables:
Table1: UserUserIdUsername
Table2: RoleRoleIdRolename
Table3: UserRoleUserIdRoleId
A User can have many Roles and a Role can have many Users.
When I model this with EF, I get a User entity with a list of UserRole entities. What I want is a User with a list of Role entities.
Is there a way to model this or query via LINQ to return a User entity and the Role entities they belong to?
Thanks
Dirk
If you model a many-to-many relation, the table in the middle will not appear in your conceptual model. (i.e. you will not have a class "UserRole" derived from "EntityObject")
If you use the EF wizard, ensure that your table "UserRole" only have these two Fields and no others. Also ensure, that you have created the foreign key constraints on both of the fields. if you have, then the wizard will create a proper many-to-many relation.
The query then probably looks something like
using(MyObjectContext context = new MyObjectContext(someParameters)){
var theUser = (from user in context.UserSet
where user.UserId = XY
select user).First();
theUser.Roles.Load();
}