我正在尝试将以下PostgreSQL查询转换为Sequelize:
select reviews.product_id, products.brand
from reviews inner join products
on reviews.product_id = any(products.product_id);reviews.product_id是一个varchar,而products.product_id是一个varchar数组,这就是我使用any运算符的原因。我创建了这个Sequelize查询,试图复制上面的查询,但它只返回一个空对象。
db.reviews.findAll({
attributes: ["product_id"],
include: [{
model: db.products,
as: "products",
on: {
col1: where(col("reviews.product_id"), "=", {[Op.contains]: [col("products.product_id")]})
},
required: true,
attributes: ["brand"]
}],
limit: 10
})
.then(reviews => res.json(reviews))
.catch(err => res.json(err));PostgreSQL查询按照预期工作,所以我知道它应该返回一些东西。对于上下文,我在我的reviews模型中设置了以下belongsTo关联:
myModel.associate = models => {
myModel.belongsTo(models.products, {foreignKey: "product_id", targetKey: "product_id", as: "products"});
};我不知道如何准确地指定我正在连接不同的类型(varchar和varchar数组)以获得我想要的结果。有没有人知道解决方案?
发布于 2018-11-15 23:52:19
我就想明白了。我用错了接线员。我刚刚将include中的on参数从:
on: {
col1: where(col("reviews.product_id"), "=", {[Op.contains]: [col("products.product_id")]})
}至:
on: {
col1: where(col("reviews.product_id"), "=", {[Op.any]: [col("products.product_id")]})
}https://stackoverflow.com/questions/53322743
复制相似问题