flask多对多关系报错,sqlalchemy.exc.NoForeignKeysError

Laqide 2017-12-23 09:58:22
book_author = db.Table("bookrelathons", db.Column("id", db.Integer, db.ForeignKey("author.id")), db.Column("id", db.Integer, db.ForeignKey("books.id")))

class Books(db.Model):
__tablename__="books"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))
author = db.relationship("Author", backref="book", secondary=book_author)

class Author(db.Model):
__tablename__="author"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))

诸位大神,请帮我看看这段代码为什么会报错,错误是sqlalchemy.exc.NoForeignKeysError
NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Books.author - there are no foreign keys linking these tables via secondary table 'bookrelathons'. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.
...全文
738 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Laqide 2017-12-24
  • 打赏
  • 举报
回复
自己解决了。 book_author = db.Table("bookrelathons", db.Column("id", db.Integer, db.ForeignKey("author.id")), db.Column("id", db.Integer, db.ForeignKey("books.id"))) 标红的名字不能重复。改成author_id和book_id就可以了。

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧