50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
# @Time : 2024/11/18 下午7:00
|
|||
|
|
# @Author : 河瞬
|
|||
|
|
# @FileName: models.py
|
|||
|
|
# @Software: PyCharm
|
|||
|
|
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Enum, ForeignKey, Text
|
|||
|
|
from sqlalchemy.orm import relationship, sessionmaker, declarative_base
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
# 创建基类
|
|||
|
|
Base = declarative_base()
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Tenant(Base):
|
|||
|
|
__tablename__ = 'Tenant'
|
|||
|
|
id = Column(Integer, primary_key=True)
|
|||
|
|
name = Column(String(100), nullable=False)
|
|||
|
|
users = relationship('User', backref='Tenant', cascade='all, delete-orphan')
|
|||
|
|
projects = relationship('Project', backref='Tenant', cascade='all, delete-orphan')
|
|||
|
|
|
|||
|
|
|
|||
|
|
class User(Base):
|
|||
|
|
__tablename__ = 'User'
|
|||
|
|
id = Column(Integer, primary_key=True)
|
|||
|
|
name = Column(String(100), nullable=False)
|
|||
|
|
password = Column(String(100), nullable=False)
|
|||
|
|
role = Column(Integer, nullable=False) # 0: 超级管理员 1:租户管理员 2:评估员 3:审核员
|
|||
|
|
tenant_id = Column(Integer, ForeignKey('Tenant.id'), nullable=False)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Project(Base):
|
|||
|
|
__tablename__ = 'Project'
|
|||
|
|
id = Column(Integer, primary_key=True)
|
|||
|
|
name = Column(String(100), nullable=False)
|
|||
|
|
requirement = Column(String(200), nullable=True)
|
|||
|
|
start_time = Column(DateTime, nullable=False, default=datetime.utcnow)
|
|||
|
|
deadline = Column(DateTime, nullable=False)
|
|||
|
|
owner_id = Column(Integer, ForeignKey('Tenant.id'), nullable=False)
|
|||
|
|
# owner = relationship('Tenant', backref='projects')
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
# 创建数据库引擎
|
|||
|
|
engine = create_engine('sqlite:///test.db')
|
|||
|
|
|
|||
|
|
# 创建所有表
|
|||
|
|
Base.metadata.create_all(engine)
|
|||
|
|
|
|||
|
|
# 创建会话
|
|||
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|