Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一对多关系上的NoForeignKeysError-SQLAlchemy_Python_Database_Sqlite_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python 一对多关系上的NoForeignKeysError-SQLAlchemy

Python 一对多关系上的NoForeignKeysError-SQLAlchemy,python,database,sqlite,sqlalchemy,flask-sqlalchemy,Python,Database,Sqlite,Sqlalchemy,Flask Sqlalchemy,我正在用Flask和Flask SQLAlchemy编写一个简单电子商务站点的后端,但是我在创建SQLAlchemy数据库时遇到了问题 DB图如下所示 我为我的数据库编写了模型,但是当我尝试测试它们时,我得到了关于父项和订单项之间的一对多关系的错误消息: sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Product.

我正在用Flask和Flask SQLAlchemy编写一个简单电子商务站点的后端,但是我在创建SQLAlchemy数据库时遇到了问题

DB图如下所示

我为我的数据库编写了模型,但是当我尝试测试它们时,我得到了关于父项订单项之间的一对多关系的错误消息:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Product.order_item - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
模型如下:

类OrderItem(db.Model):
__tablename_uu='OrderItem'
订单\项目\编号=db.Column(db.Integer(),主键=True)
订单数量=db.Column(db.Integer())
order\u item\u product\u number=db.Column(db.Integer(),db.ForeignKey('product.product\u number'))
order\u item\u order\u number=db.Column(db.Integer(),db.ForeignKey('order.order\u number'))
定义报告(自我):
返回“”。格式(self.order\u item\u编号、self.order\u order\u item)
类别产品(数据库模型):
_tablename_uu=‘产品’
product\u number=db.Column(db.Integer(),主键=True)
product\u availability=db.Column(db.Boolean())
product_price=db.Column(db.Integer())
product_unit=db.Column(db.Integer())
product_折扣=db.Column(db.Integer(),默认值=0)
order\u item=db.relationship('OrderItem',backref='Product')
定义报告(自我):

return“
\u tablename\u=”Product“
似乎缺少一个前导下划线,这意味着Flask SQLAlchemy可能正在将该表的名称设置为
'Product'
,小写字母为“p”,这意味着
db.ForeignKey('Product.Product\u number')
实际上没有引用您的产品表。。我还没有测试过,但似乎有道理。sqlite模式不区分大小写。也许这个案例对SQLAlchemy很重要,但对数据库不重要。在
return'中缺少一个
括号似乎@supershot是对的,我将每个表名都改为小写的pep8,问题似乎消失了。现在我得到了一个
sqlalchemy.orm.exc.UnmappedInstanceError:Class'builtins.module'未映射
,但这应该是因为我在模型定义中导入了错误的db实例。很高兴有些东西起了作用,但是你得到了Supershot的注释吗?他不建议更改大小写,而是建议在
\u tablename\uuuu
中添加下划线以使其一致。他只提到桌子的情况是打字错误的副作用。您仍然应该修复我们指出的两个打字错误。