class Posts(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.Text) pid = db.Column(db.Integer, default=0) path = db.Column(db.String(255), default='0,') #记录时间的 timestamp = db.Column(db.DateTime, default=datetime.utcnow) #user的外键 uid = db.Column(db.Integer, db.ForeignKey('user.id'))
class Posts(db.Model): id = db.Column(db.Integer, primary_key=True) content = db.Column(db.Text) title = db.Column(db.String(50), default='电影名称') pid = db.Column(db.Integer, default=0) image = db.Column(db.String(255), default='defaultImage,') timestamp = db.Column(db.DateTime, default=datetime.utcnow) uid = db.Column(db.Integer, db.ForeignKey('user.id')) characters = db.Column(db.String(255), default='暂无详细演员信息') #演员名称 director = db.Column(db.String(255), default="暂无详细导演信息") selfread = db.Column(db.Text, default="暂无作者观后感")
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) # 博客的内容 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) # 发表时间 # 设置一对多外键 uid = db.Column(db.Integer, db.ForeignKey('user.id')) # 设置外键 关联主表user的自增id state = db.Column(db.Integer, default=0) # 是否所有人可见
class Posts(Base, db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(23), index=True) article = db.Column(db.Text) pid = db.Column(db.Integer, default=0) path = db.Column(db.Text, default='0,') fabulous = db.Column(db.Integer, default=0) times = db.Column(db.Integer, default=0) timestamp = db.Column(db.DateTime, default=datetime.utcnow) #使用ForeignKey 来创建表的外键 作用是 和数据库中的user表(用户信息表)进行关联查询 表迁移以后 表中救会有一个 uid的字段 一对多 关系 一个用户 发表多个帖子 多的表使用ForeignKey 来关联1的一方 #uid是模型Posts下的posts表单中的外键,关联的是User模型下的user表单中的id值,通过id值就可以找到对应的用户名,知道文章或评论是谁发的。 uid = db.Column(db.Integer, db.ForeignKey('user.id'))
class Posts(Base,db.Model): __tablename__ = 'posts' id = db.Column(db.Integer,primary_key=True) title = db.Column(db.String(20)) #标题 content = db.Column(db.Text) #博客内容 pid = db.Column(db.Integer,default=0) #pid为0则为博客 否则为评论和回复 path = db.Column(db.Text,default='0,') #路径path visit = db.Column(db.Integer,default=0) #访问量 timestamp = db.Column(db.DateTime,default=datetime.utcnow) #发表的时间 state = db.Column(db.Boolean,default=True) #默认所有人可见 uid = db.Column(db.Integer,db.ForeignKey('user.id')) def __str__(self): return self.title
class Posts(db.Model, Base): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(23), index=True) article = db.Column(db.Text) pid = db.Column(db.Integer, default=0) path = db.Column(db.Text, default='0,') fabulous = db.Column(db.Integer, default=0) times = db.Column(db.Integer, default=0) timestamp = db.Column(db.DateTime, default=datetime.utcnow()) scan = db.Column(db.Integer, default=0) #外键,与user表主键id关联,进行关联查询 #表迁移后 表中会有一个uid字段 一对多 一个用户发表多个帖子 uid = db.Column(db.Integer, db.ForeignKey('user.id'))
class Email(db.Model): __tablename__ = 'emails' id = db.Column(db.Integer(), primary_key=True) email = db.Column(db.String(64)) # 一对多 # 声明外键 一般放在多的一方 # db.ForeignKey(表名.主键) 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
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, backref=db.backref('posts')) u_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 关联主表user的自增id
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')), )
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,
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) # 双向关系
from .user import User from .posts import Posts from .course import Course from App.extensions import db #收藏帖子表,user和posts共用的中间表,在init中创建 collections = db.Table('collections',db.Column('user_id',db.Integer,db.ForeignKey('user.id')),db.Column('posts_id',db.Integer,db.ForeignKey('posts.id')))
from .user import User from .movie import Movie from App.extensions import db collections = db.Table('collections', db.Column('user_id',db.Integer,db.ForeignKey('user.id')), db.Column('movie_id',db.Integer,db.ForeignKey('movie.id')), )