Skip to content

数据表

在创建数据模型之前,先创建一个模型基类,封装一些通用的方法。

python
from forum.extensions import db


class BaseORM(db.Model):
    __abstract__ = True

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    def as_json(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

用户

User 类表示用户模型,用户模型包含存储用户信息和博客资料的字段:

  • 主键字段:id
  • 用户姓名:nick_name
  • 密码散列值:password_hash
  • 手机号:mobile
  • 用户头像路径:avatar_url
  • 最后一次登录时间:最后一次登录时间
  • 是否是管理员:is_admin
  • 用户签名:signature
  • 用户性别:gender

这些字段我们会在后面使用到的时候具体介绍。

python
from datetime import datetime

import sqlalchemy as sa

from forum.orms._base import BaseORM


class UserORM(BaseORM):
    """用户"""

    __tablename__ = "forum_user"

    id = sa.Column(sa.Integer, primary_key=True, comment="用户编号")
    nickname = sa.Column(sa.String(32), unique=True, nullable=False, comment="用户昵称")
    password_hash = sa.Column(sa.String(255), nullable=False, comment="加密的密码")
    mobile = sa.Column(sa.String(11), unique=True, nullable=False, comment="手机号")
    email = sa.Column(sa.String(50), comment="用户头像路径")
    avatar_url = sa.Column(sa.String(256), comment="用户头像路径")
    is_admin = sa.Column(sa.Boolean, default=False, comment="是否是管理员")
    signature = sa.Column(sa.String(512), comment="用户签名")

    gender = sa.Column(sa.Enum("男", "女"), default="男", comment="性别")

    create_at = sa.Column(sa.DateTime, default=datetime.utcnow, comment="创建时间")
    update_at = sa.Column(
        sa.DateTime,
        default=datetime.utcnow,
        onupdate=datetime.utcnow,
        comment="记录的更新时间",
    )

分类

用于存储文章分类的数据库模型

python
from datetime import datetime

import sqlalchemy as sa

from forum.orms._base import BaseORM


class CategoryORM(BaseORM):
    """文章分类"""

    __tablename__ = "forum_category"

    id = sa.Column(sa.Integer, primary_key=True, comment="分类编号")
    name = sa.Column(sa.String(64), nullable=False, comment="分类名")

    create_at = sa.Column(sa.DateTime, default=datetime.utcnow, comment="创建时间")
    update_at = sa.Column(
        sa.DateTime,
        default=datetime.utcnow,
        onupdate=datetime.utcnow,
        comment="记录的更新时间",
    )

分类的名称不允许重复,因此 name 字段将 unique 参数设为 True。

文章

存储文章 Post 模型

python
from datetime import datetime

import sqlalchemy as sa

from forum.orms._base import BaseORM


class ArticleORM(BaseORM):
    """文章"""

    __tablename__ = "forum_article"

    id = sa.Column(sa.Integer, primary_key=True, comment="文章编号")
    title = sa.Column(sa.String(256), nullable=False, comment="文章标题")
    source = sa.Column(sa.String(64), nullable=False, comment="文章来源")
    digest = sa.Column(sa.String(512), nullable=False, comment="文章摘要")
    content = sa.Column(sa.Text, nullable=False, comment="文章内容")
    clicks = sa.Column(sa.Integer, default=0, comment="浏览量")
    index_image_url = sa.Column(sa.String(256), comment="文章列表图片路径")
    status = sa.Column(
        sa.Integer, default=0, comment="文章状态 如果为 0 代表审核通过,1 代表审核中,-1 代表审核不通过"
    )
    reason = sa.Column(sa.String(256), comment="未通过原因,status = -1 的时候使用")

    category_id = sa.Column(sa.Integer, sa.ForeignKey("forum_category.id"))
    user_id = sa.Column(sa.Integer, sa.ForeignKey("forum_user.id"))

    create_at = sa.Column(sa.DateTime, default=datetime.utcnow, comment="创建时间")
    update_at = sa.Column(
        sa.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
    )

评论

用来存储评论的模型类 Comment

python
from datetime import datetime

import sqlalchemy as sa

from forum.orms._base import BaseORM


class CommentORM(BaseORM):
    """评论"""

    __tablename__ = "forum_comment"

    id = sa.Column(sa.Integer, primary_key=True, comment="评论编号")
    content = sa.Column(sa.Text, nullable=False, comment="评论内容")
    parent_id = sa.Column(
        sa.Integer, sa.ForeignKey("forum_comment.id"), comment="父评论id"
    )
    like_count = sa.Column(sa.Integer, default=0, comment="点赞条数")
    user_id = sa.Column(sa.Integer, sa.ForeignKey("forum_user.id"), comment="用户id")
    article_id = sa.Column(sa.Integer, sa.ForeignKey("forum_article.id"), comment="文章id")

    create_at = sa.Column(sa.DateTime, default=datetime.utcnow, comment="创建时间")
    update_at = sa.Column(
        sa.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间"
    )

最后将所有的模型导入到 __init__.py

python
from .article import ArticleORM
from .category import CategoryORM
from .comment import CommentORM
from user import UserORM

__all__ = [ArticleORM, CategoryORM, CommentORM, UserORM]