from App.extensions import db from .db_base import DB_Base from datetime import datetime posts_categorys = db.Table( 'posts_categorys', db.Column('posts_id', db.Integer, db.ForeignKey('posts.id'), primary_key=True), db.Column('categorys_id', db.Integer, db.ForeignKey('categorys.id'), primary_key=True)) # 博客模型 class Posts(DB_Base, db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(20), index=True) # 标题 article = db.Column(db.Text) # md格式正文 pid = db.Column(db.Integer, default=0) # 父id path = db.Column(db.Text, default='0,') # 路径 visit = db.Column(db.Integer, default=0) # 文章访问量 timestamp = db.Column(db.DateTime, default=datetime.utcnow) # 发表时间 state = db.Column(db.Integer, default=0) # 是否所有人可见 img = db.Column(db.String(70)) # 图片 tags = db.relationship('Categorys', secondary=posts_categorys,
from .user import User from .posts import Posts from App.extensions import db # 创建多对多中间表 存储多对多的id collections = db.Table( 'collections', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('posts_id', db.Integer, db.ForeignKey('posts.id')), )
t_id = db.Column(db.Integer, db.ForeignKey('teachers.id')) # Teacher.mail << ==== >>Email.teacher 双向关系 # 反向查询 teacher = db.relationship('Teacher', backref='mail') def __repr__(self): return self.email # 使用中间表关联 多对对 article_tag = db.Table( 'article_tag', # 表名 db.Column('article_id', db.Integer(), db.ForeignKey('article.id'), primary_key=True), db.Column('tag_id', db.Integer(), db.ForeignKey('tag.id'), primary_key=True)) # 文章 class Article(db.Model): __tablename__ = 'article' id = db.Column(db.Integer(), primary_key=True) # 书名 title = db.Column(db.String(32), nullable=False) # 双向关系 tag = db.relationship('Tag', secondary=article_tag, backref='article')