-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
36 lines (23 loc) · 1009 Bytes
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sqlalchemy import Column, Integer, String, Table, ForeignKey
from sqlalchemy.orm import relationship
from mydatabase import Base
# Association table for many-to-many relationship
user_roles_table = Table('user_roles', Base.metadata,
Column('user_id', Integer, ForeignKey('users.id')),
Column('role_id', Integer, ForeignKey('roles.id'))
)
class Role(Base):
__tablename__ = 'roles'
id = Column(Integer, primary_key=True, autoincrement=True)
role_name = Column(String, unique=True)
def __repr__(self):
return f"<Role(role_name='{self.role_name}')>"
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
username = Column(String, unique=True, index=True)
hashed_password = Column(String)
# Many-to-many relationship with Role
roles = relationship("Role", secondary=user_roles_table, backref="users")
def __repr__(self):
return f"<User(username='{self.username}')>"