class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) can_comment = db.Column(db.Boolean, default=True) author_id = db.Column(db.Integer, db.ForeignKey('admin.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all') author = db.relationship('Admin', back_populates='posts')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30)) timestamp = db.Column(db.DateTime, default=datetime.utcnow) # 时间戳 body = db.Column(db.Text) can_comments = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) # 将category_id设置为外键 category = db.relationship('Category', back_populates='posts') admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) admin = db.relationship('Admin', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan') # cascade设置级联操作
class Comments(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) posts_id = db.Column(db.Integer, db.ForeignKey('ports.id')) posts = db.relationship('Post', back_populates='comments') replied_id = db.Column(db.Integer, db.ForeignKey('comments.id')) replies = db.relationship('Comments', back_populates='replied', cascade='all, delete-orphan') replied = db.relationship('Comments', back_populates='replies', remote_side=[id])
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author_name = db.Column(db.String(30)) body = db.Column(db.Text) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) author_id = db.Column(db.Integer, db.ForeignKey('admin.id')) replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post_id_r = db.Column(db.Integer) author = db.relationship('Admin', back_populates='comments') post = db.relationship('Post', back_populates='comments') replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(20)) timestamp = db.Column(db.DateTime, default=datetime.utcnow) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 建立邻接列表关系(在同一个模型内的一对多关系) replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) # replied表示被回复评论的标量关系属性, remote_side=[id]将id字段定义为关系远程侧 replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) replies = db.relationship('Comment', back_populates='replied', cascade='all')