@@ -24,28 +24,58 @@ class ValidPermissions(Enum):
24
24
EDIT_ROLE = "Edit Role"
25
25
26
26
27
+ class UserOrganizationLink (SQLModel , table = True ):
28
+ id : Optional [int ] = Field (default = None , primary_key = True )
29
+ user_id : int = Field (foreign_key = "user.id" )
30
+ organization_id : int = Field (foreign_key = "organization.id" )
31
+ role_id : int = Field (foreign_key = "role.id" )
32
+ created_at : datetime = Field (default_factory = utc_time )
33
+ updated_at : datetime = Field (default_factory = utc_time )
34
+
35
+ user : "User" = Relationship (back_populates = "organization_links" )
36
+ organization : "Organization" = Relationship (back_populates = "user_links" )
37
+ role : "Role" = Relationship (back_populates = "user_links" )
38
+
39
+
40
+ class RolePermissionLink (SQLModel , table = True ):
41
+ id : Optional [int ] = Field (default = None , primary_key = True )
42
+ role_id : int = Field (foreign_key = "role.id" )
43
+ permission_id : int = Field (foreign_key = "permission.id" )
44
+ created_at : datetime = Field (default_factory = utc_time )
45
+ updated_at : datetime = Field (default_factory = utc_time )
46
+
47
+
27
48
class Organization (SQLModel , table = True ):
28
49
id : Optional [int ] = Field (default = None , primary_key = True )
29
50
name : str
30
51
created_at : datetime = Field (default_factory = utc_time )
31
52
updated_at : datetime = Field (default_factory = utc_time )
32
53
deleted : bool = Field (default = False )
33
54
34
- users : List ["User" ] = Relationship (back_populates = "organization" )
55
+ user_links : List [UserOrganizationLink ] = Relationship (
56
+ back_populates = "organization" )
57
+ users : List ["User" ] = Relationship (
58
+ back_populates = "organizations" ,
59
+ link_model = UserOrganizationLink
60
+ )
61
+ roles : List ["Role" ] = Relationship (back_populates = "organization" )
35
62
36
63
37
64
class Role (SQLModel , table = True ):
38
65
id : Optional [int ] = Field (default = None , primary_key = True )
39
66
name : str
40
- organization_id : Optional [int ] = Field (
41
- default = None , foreign_key = "organization.id" )
67
+ organization_id : int = Field (foreign_key = "organization.id" )
42
68
created_at : datetime = Field (default_factory = utc_time )
43
69
updated_at : datetime = Field (default_factory = utc_time )
44
70
deleted : bool = Field (default = False )
45
71
46
- users : List [ "User" ] = Relationship (back_populates = "role " )
47
- role_permission_links : List ["RolePermissionLink" ] = Relationship (
72
+ organization : Organization = Relationship (back_populates = "roles " )
73
+ user_links : List [UserOrganizationLink ] = Relationship (
48
74
back_populates = "role" )
75
+ permissions : List ["Permission" ] = Relationship (
76
+ back_populates = "roles" ,
77
+ link_model = RolePermissionLink
78
+ )
49
79
50
80
51
81
class Permission (SQLModel , table = True ):
@@ -56,21 +86,10 @@ class Permission(SQLModel, table=True):
56
86
updated_at : datetime = Field (default_factory = utc_time )
57
87
deleted : bool = Field (default = False )
58
88
59
- role_permission_links : List ["RolePermissionLink" ] = Relationship (
60
- back_populates = "permission" )
61
-
62
-
63
- class RolePermissionLink (SQLModel , table = True ):
64
- id : Optional [int ] = Field (default = None , primary_key = True )
65
- role_id : Optional [int ] = Field (
66
- default = None , foreign_key = "role.id" )
67
- permission_id : Optional [int ] = Field (
68
- default = None , foreign_key = "permission.id" )
69
-
70
- role : Optional ["Role" ] = Relationship (
71
- back_populates = "role_permission_links" )
72
- permission : Optional ["Permission" ] = Relationship (
73
- back_populates = "role_permission_links" )
89
+ roles : List ["Role" ] = Relationship (
90
+ back_populates = "permissions" ,
91
+ link_model = RolePermissionLink
92
+ )
74
93
75
94
76
95
class PasswordResetToken (SQLModel , table = True ):
@@ -92,23 +111,15 @@ class User(SQLModel, table=True):
92
111
email : str = Field (index = True , unique = True )
93
112
hashed_password : str
94
113
avatar_url : Optional [str ] = None
95
- organization_id : Optional [int ] = Field (
96
- default = None , foreign_key = "organization.id" )
97
- role_id : Optional [int ] = Field (default = None , foreign_key = "role.id" )
98
114
created_at : datetime = Field (default_factory = utc_time )
99
115
updated_at : datetime = Field (default_factory = utc_time )
100
116
deleted : bool = Field (default = False )
101
117
102
- organization : Optional ["Organization" ] = Relationship (
103
- back_populates = "users" )
104
- role : Optional ["Role" ] = Relationship (back_populates = "users" )
118
+ organization_links : List [UserOrganizationLink ] = Relationship (
119
+ back_populates = "user" )
120
+ organizations : List ["Organization" ] = Relationship (
121
+ back_populates = "users" ,
122
+ link_model = UserOrganizationLink
123
+ )
105
124
password_reset_tokens : List ["PasswordResetToken" ] = Relationship (
106
125
back_populates = "user" )
107
-
108
-
109
- class UserOrganizationLink (SQLModel , table = True ):
110
- id : Optional [int ] = Field (default = None , primary_key = True )
111
- user_id : Optional [int ] = Field (
112
- default = None , foreign_key = "user.id" )
113
- organization_id : Optional [int ] = Field (
114
- default = None , foreign_key = "organization.id" )
0 commit comments