报错:sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can’t proceed with initialization of other mappers. Triggering mapper: ‘Mapper|Food|food’. Original exception was: Could not determine join condition between parent/child tables on relationship Food. cat - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a ‘primaryjoin’ expression.
翻译:
一个或多个映射器初始化失败-不能继续初始化其他映射器。 触发映射器:“映射器|食品|食品”。 最初的例外是:无法确定关系Food的父/子表之间的连接条件。 Cat—没有外键连接这些表。 确保引用的列与ForeignKey或ForeignKeyConstraint相关联,或者指定一个’primaryjoin’表达式。
你报这个错是否是使用flask的orm框架对MySQL数据库进行一查多或者是多查一?

我遇到的这个问题是使用了flask的flask-sqlacodegen生成的模型类, 模型类中没有指定外键
还有就是使用这个必须在表中关系为一的一方方使用这个relationship
还有就是修改了模型类就得生成、执行数据库迁移文件
可以去看看我的这篇博客详细写了如何一查多,多查一,多查多
https://blog.csdn.net/qq_48082548/article/details/120344299
本文针对使用Flask的ORM框架时出现的InvalidRequestError错误进行了分析。主要原因是未正确设置模型类之间的外键关系,特别是在一查多或多查一的关系中。文章提供了详细的解决方案,并附带了一个关于如何实现不同类型查询的具体示例。

1327

被折叠的 条评论
为什么被折叠?



