-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModels.py
71 lines (52 loc) · 2.38 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from sqlalchemy import Column, Integer, TEXT
from sqlalchemy import ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base() # <-元类
# ************下列所有类都是基于Base = declarative_base()而存在的,如果使用sqlalchemy的反射机制automap_base(),可以不用定义模型层
# 定义Assignments对象
class Assignments(Base):
# 表的名字
__tablename__ = 'Assignments'
__table_args__ = {'sqlite_autoincrement': True}
ID = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
AssName = Column(TEXT, nullable=False)
MaxCount = Column(Integer)
CurrentCount = Column(Integer)
# 添加关系属性
# teams变量装载了查询到的属于该Assignment的所有小组(使用AssID外键遍历Teams表,返回teams列表)
# 加上backref字段的话,则实现在Teams对象上,通过teams1.assignment语句输出查询到的该teams1所属的Assignment集合
teams = relationship('Teams', backref='assignment')
# 定义Teams对象
class Teams(Base):
# 表的名字
__tablename__ = 'Teams'
__table_args__ = {'sqlite_autoincrement': True}
ID = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
TeamName = Column(TEXT, nullable=False)
TeamScore = Column(Integer)
VoteNum = Column(Integer)
AssID = Column(Integer, ForeignKey('Assignments.ID'))
students = relationship('Students', backref='team')
# assignment = None
# 定义Students对象,该表其实是Teams表和Majors表解除多对多关系的一个关联表
class Students(Base):
# 表的名字
__tablename__ = 'Students'
__table_args__ = {'sqlite_autoincrement': True}
ID = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
StuName = Column(TEXT, nullable=False)
StuSex = Column(TEXT)
StuNum = Column(TEXT, nullable=False)
TeamID = Column(Integer, ForeignKey('Teams.ID'))
MajorID = Column(Integer, ForeignKey('Majors.ID'))
# major = None
# team = None
# 定义Majors对象
class Majors(Base):
# 表的名字
__tablename__ = 'Majors'
__table_args__ = {'sqlite_autoincrement': True}
ID = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
MajorName = Column(TEXT, nullable=False)
students = relationship('Students', backref='major')