摘要这一篇文章是关于flask_appbuilder的一个小坑吧,关于数据库外键的设置问题,记录在这里。
文章目录(Table of Contents)
问题来源
最近在使用flask_appbuilder的时候,在使用数据库外键的时候遇到了一些报错:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship version_patch.patchcode - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
出现这个问题的原因是因为数据库设置中ForeignKey中表名设置出现了问题,我们看一下下面的解决方法。
解决思路
首先看一下原因:
原来db.ForeignKey中的‘表名.字段名'中的表名是数据库上的表名,我们可以使tablename = 'VERSION_PATCH'来指定表名字。
我们来看一个例子:
# 服务商分组表
class Providers(Model):
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True, nullable=False)
def __repr__(