-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
45 lines (33 loc) · 1.65 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
from exts import db
from datetime import datetime
class UserModel(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(100), unique=True, nullable=False)
password = db.Column(db.String(500), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
join_time = db.Column(db.DateTime, default=datetime.now)
class EmailCaptchaModel(db.Model):
__tablename__ = 'email_captcha'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(100), unique=True, nullable=False)
captcha = db.Column(db.String(100), nullable=False)
class PostModel(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
author = db.relationship('UserModel', backref='posts')
class CommentModel(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
content = db.Column(db.Text, nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now)
# ForeignKey
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# Relationship
post = db.relationship('PostModel', backref=db.backref('comments', order_by=create_time.desc()))
author = db.relationship('UserModel', backref='comments')