V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
honmaple
V2EX  ›  Flask

请教 sqlalchemy 一对多模型问题?

  •  
  •   honmaple · 2015-12-10 14:24:21 +08:00 · 2251 次点击
    这是一个创建于 3059 天前的主题,其中的信息可能已经有所发展或是发生改变。
    评论功能,计划一条评论对应多个回复,但是看文档有些地方不太清楚
    部分源码
    class Comments(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer,primary_key=True)
    comment_user = db.Column(db.String, nullable=False)
    comment_publish = db.Column(db.DateTime, nullable=False)
    comment_content = db.relationship('Replies',backref='comments',lazy='dynamic')

    def __init__(self, comment_user,comment_content, comment_publish = datetime.datetime.now()):
    self.comment_user = comment_user
    self.comment_content = comment_content
    self.comment_publish = comment_publish

    def __repr__(self):
    return "<Comments %r>" % self.comment_content

    class Replies(db.Model):
    __tablename__ = 'replies'
    id = db.Column(db.Integer,primary_key=True)
    reply_user = db.Column(db.String, nullable=False)
    reply_publish = db.Column(db.DateTime, nullable=False)
    reply_content = db.Column(db.Text,nullable=False)
    comments_id = db.Column(db.Integer, db.ForeignKey('comments.id'))

    def __init__(self, reply_user,reply_content, reply_publish = datetime.datetime.now()):
    self.reply_user = reply_user
    self.reply_content = reply_content
    self.reply_publish = reply_publish

    def __repr__(self):
    return "<Replies %r>" % self.reply_content
    使用这个创建表后利用
    all_comment = Comments.query.join(Replies).all()
    查询,报错
    sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: comments [SQL: 'SELECT comments.id AS comments_id, comments.comment_user AS comments_comment_user, comments.comment_publish AS comments_comment_publish \nFROM comments JOIN replies ON comments.id = replies.comments_id']
    确认已经创建表,请教大神这个应该怎么弄
    7 条回复    2015-12-12 20:14:42 +08:00
    keakon
        1
    keakon  
       2015-12-10 16:41:56 +08:00
    你没有创建表。
    honmaple
        2
    honmaple  
    OP
       2015-12-10 17:18:49 +08:00
    @keakon 确认创建了,后来加了__bind_key__ 倒是没报错了,但数据表的关系上还是不清楚
    neoblackcap
        3
    neoblackcap  
       2015-12-10 17:26:49 +08:00
    @honmaple 你确定你连接的那个数据里面有表?会不会连接到开发测试数据库?
    Shared
        4
    Shared  
       2015-12-10 17:30:02 +08:00
    你看报错信息里已经打印出 SQL 语句了,你复制 SQL 语句在 MySQL 控制台里面运行一下,看看什么结果
    honmaple
        5
    honmaple  
    OP
       2015-12-10 17:40:34 +08:00
    @neoblackcap @Shared 报错的问题我通过加__bind_key__='comments'和__bind_key__='replies'解决了,我想问下通过上述代码创建的两个表是不是关系表,插入数据时 comments_id 该怎么插入,能麻烦看一下吗
    Shared
        6
    Shared  
       2015-12-10 21:49:43 +08:00
    @honmaple 能在提供完整的代码,要不然没办法给你分析
    honmaple
        7
    honmaple  
    OP
       2015-12-12 20:14:42 +08:00
    @Shared 多谢了,我差不多理解已经了一对多,多对多的关系
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2666 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:59 · PVG 23:59 · LAX 08:59 · JFK 11:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.