Flask SQLAlchemy,错误与多对多的关系

时间:2015-01-13 07:17:14

标签: python flask sqlalchemy flask-sqlalchemy

尝试创建数据库并收到以下错误:

File "XXXXX/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1987, in _determine_joins
    "expressions." % (self.prop, self.secondary)) NoForeignKeysError: Could not determine join condition between parent/child tables on relationship PostReply.tags - there are no foreign keys linking these tables via secondary table 'postReplyTags'.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.

关于这个数据库和相关部分的更多信息。它有帖子和帖子回复,你可以添加标签。

它的设置如下:

postTags = db.Table('postTags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)

postReplyTags = db.Table('postReplyTags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('postreply_id', db.Integer, db.ForeignKey('postreply.id'))
)

class Post(db.Model):
    __searchable__ = ['post_text']

    id = db.Column(db.Integer, primary_key=True)
    post_text = db.Column(db.String(256))
    tags = db.Column(db.String(64))
    #post_image: to be added
    post_time = db.Column(db.Integer)
    post_reply_number = db.Column(db.Integer) #to be taken out
    number_of_replies = db.Column(db.Integer)
    post_score = db.Column(db.Integer)
    post_rank = db.Column(db.Integer)
    flagged = db.Column(db.Integer)
    admin_post = db.Column(db.Boolean)
    company_name = db.Column(db.String(64))

    tagged = db.relationship('Tag', secondary=postTags,
        backref=db.backref('posts', lazy='dynamic'))
    #has user
    user_id = db.Column(db.String, db.ForeignKey('user.email'))

    def __repr__(self):
        return '<User %r>' % self.post_text

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class PostReply(db.Model):
    __searchable__ = ['reply_text']

    id = db.Column(db.Integer, primary_key=True)
    associated_post_id = db.Column(db.Integer)
    reply_text = db.Column(db.String(256))
    tags = db.Column(db.String(64))
    #post_image: to be added
    reply_time = db.Column(db.Integer)
    flagged = db.Column(db.Integer)
    #has user
    user_id = db.Column(db.String, db.ForeignKey('user.email'))
    tags = db.relationship('Tag', secondary=postReplyTags,
        backref=db.backref('post_replies', lazy='dynamic'))

    def __repr__(self):
        return '<User %r>' % self.post_text

    def as_dict(self):
      return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    postreply_id = db.Column(db.Integer, db.ForeignKey('postreply.id'))
    name = db.Column(db.String(64))

奇怪的是,帖子的所有内容都有效,但帖子的回复却没有。任何人都可以帮我弄清楚为什么会这样吗?这是标签和帖子/帖子回复之间的多对多关系?非常感谢!

1 个答案:

答案 0 :(得分:2)

事实证明,驼峰的情况不起作用,需要用下划线转换。例如,postReply to post_reply