-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
80 lines (62 loc) · 2.79 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
72
73
74
75
76
77
78
79
80
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Table
from sqlalchemy.orm import relationship
from database import Base
from pydantic import validator
question_voter = Table(
'question_voter',
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id'), primary_key=True),
Column('question_id', Integer, ForeignKey('question.id'), primary_key=True)
)
answer_voter = Table(
'answer_voter',
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id'), primary_key=True),
Column('answer_id', Integer, ForeignKey('answer.id'), primary_key=True)
)
class Question(Base):
__tablename__ = "question"
id = Column(Integer, primary_key=True)
subject = Column(String, nullable=False)
content = Column(Text, nullable=False)
create_date = Column(DateTime, nullable=False)
user_id = Column(Integer, ForeignKey("user.id"), nullable=True)
user = relationship("User", backref="question_users")
modify_date = Column(DateTime, nullable=True)
voter = relationship('User', secondary=question_voter, backref='question_voters')
category_id = Column(Integer, ForeignKey("category.id"), nullable=True)
category = relationship("Category", backref="questions")
class Answer(Base):
__tablename__ = "answer"
id = Column(Integer, primary_key=True)
content = Column(Text, nullable=False)
create_date = Column(DateTime, nullable=False)
question_id = Column(Integer, ForeignKey("question.id"))
question = relationship("Question", backref="answers")
user_id = Column(Integer, ForeignKey("user.id"), nullable=True)
user = relationship("User", backref="answer_users")
modify_date = Column(DateTime, nullable=True)
voter = relationship('User', secondary=answer_voter, backref='answer_voters')
voter_count = Column(Integer, default=0)
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username = Column(String, unique=True, nullable=False)
password = Column(String, nullable=False)
email = Column(String, unique=True, nullable=False)
class Comment(Base):
__tablename__ = "comment"
id = Column(Integer, primary_key=True)
content = Column(Text, nullable=False)
create_date = Column(DateTime, nullable=False)
question_id = Column(Integer, ForeignKey("question.id"))
question = relationship("Question", backref="comments")
answer_id = Column(Integer, ForeignKey("answer.id"))
answer = relationship("Answer", backref="comments")
user_id = Column(Integer, ForeignKey("user.id"), nullable=True)
user = relationship("User", backref="comment_users")
modify_date = Column(DateTime, nullable=True)
class Category(Base):
__tablename__ = "category"
id = Column(Integer, primary_key=True)
subject = Column(String, nullable=False)