Study/models.py
2024-11-20 12:37:58 +08:00

32 lines
1.4 KiB
Python

from app import db
from datetime import datetime
from flask_login import UserMixin
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
password_hash = db.Column(db.String(255), nullable=False)
phone_number = db.Column(db.String(15), unique=True, nullable=False)
role = db.Column(db.Enum('guest', 'user', 'author', 'admin'), default='user')
created_at = db.Column(db.DateTime, default=datetime.utcnow)
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
content = db.Column(db.Text, nullable=False)
image_url = db.Column(db.String(255))
category = db.Column(db.String(50))
author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
article_id = db.Column(db.Integer, db.ForeignKey('article.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
parent_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# 其他模型类似定义