如何修复:编译 SELECT 查询时检测到未定义的绑定
Posted
技术标签:
【中文标题】如何修复:编译 SELECT 查询时检测到未定义的绑定【英文标题】:How to fix: Undefined binding(s) detected when compiling SELECT query 【发布时间】:2019-06-01 15:20:12 【问题描述】:我试图获取与具有特定类别的产品相关的所有具有相关功能的功能键,但收到如下所述的错误。我做错了什么?
我的表结构:
Product:
id -> int
category_id -> int (one-to-many)
FeatureProducts: (many-to-many)
id -> int
product_id -> int
feature_id -> int
Feature:
id -> int
value -> string
featurekey_id -> int (one-to-many)
FeatureKeys:
id -> int
type -> string
name -> string
text -> string
Product.query((qb) =>
qb.where('category_id', '=', params.category);
)
.belongsToMany(Featurekey)
.through(Feature)
.fetch(
withRelated: filters.populate || populate,
);
Error: Undefined binding(s) detected when compiling SELECT query: select `featurekey`.*, `feature`.`id` as `_pivot_id`, `feature`.`product_id` as `_pivot_product_id`, `feature`.`featurekey_id` as `_pivot_featurekey_id` from `featurekey` inner join `feature` on `feature`.`featurekey_id` = `featurekey`.`id` where `feature`.`product_id` = ?
【问题讨论】:
【参考方案1】:尝试更改您的代码
Product.query((qb) =>
qb.where('category_id', req.params.category);
)
.belongsToMany(Featurekey)
.through(Feature)
.fetch(
withRelated: filters.populate || populate,
);
而不是这个:
Product.query((qb) =>
qb.where('category_id', '=', params.category);
)
.belongsToMany(Featurekey)
.through(Feature)
.fetch(
withRelated: filters.populate || populate,
);
【讨论】:
以上是关于如何修复:编译 SELECT 查询时检测到未定义的绑定的主要内容,如果未能解决你的问题,请参考以下文章