数据库用 MySQL ,使用外键关联, ORM 是 Flask 里的 SQLALchemy 。
用户表( User ): id, username
文章表(Post): id, content
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
评论表( Comment ): id, content, post_author_id
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
comment_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
现在用户李四对用户张三发表的某一篇文章进行评论,比如:
http://myblog.com/post/<postid>
这个时候,我希望在存储李四对张三文章进行评论的同时,根据<postid>查出 post 的 user_id ,把该 user_id 存到 Comment 表的 post_authorid 中(就是说评论表 Comment 中,既保存了评论人李四的 id ,也保存了原文章作者张三的 id),这句 SQLALchemy 语句要怎么写呢?
post_author_id = ?
