-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
49 lines (39 loc) · 2.28 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
from jump2 import db
# N:N relation table for "i like" func
question_voter = db.Table(
'question_voter',
db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True),
db.Column('question_id', db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'), primary_key=True)
)
answer_voter = db.Table(
'answer_voter',
db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True),
db.Column('answer_id', db.Integer, db.ForeignKey('answer.id', ondelete='CASCADE'), primary_key=True)
)
# basic table
class Question(db.Model) :
id = db.Column(db.Integer, primary_key = True)
subject = db.Column(db.String(200), nullable = False)
content = db.Column(db.Text(), nullable = False)
create_date = db.Column(db.DateTime(), nullable = False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False)
user = db.relationship('User', backref=db.backref('question_set'))
modify_date = db.Column(db.DateTime(), nullable=True)
## store voter(vote user) data in secondary=question_voter table, fake column
voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set'))
class Answer(db.Model) :
id = db.Column(db.Integer, primary_key = True)
question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'))
question = db.relationship('Question', backref = db.backref('answer_set'))
content = db.Column(db.Text(), nullable = False)
create_date = db.Column(db.DateTime(), nullable = False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False)
user = db.relationship('User', backref=db.backref('answer_set'))
modify_date = db.Column(db.DateTime(), nullable=True)
## store voter(vote user) data in secondary=answer_voter table, fake column
voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
class User(db.Model) :
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(150), unique=True, nullable=False)
password = db.Column(db.String(200), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)