gpt4 book ai didi

sequelize.js - 使用 sequelize.where 重复列名

转载 作者:行者123 更新时间:2023-12-03 22:35:50 25 4
gpt4 key购买 nike

你在做什么?

我正在尝试在包含的模型中使用带有 sequelize.where 的函数

发布重现问题的最小代码示例,包括模型和关联


class Promotion extends SZ.Model {}

Promotion.init({
id: {
type: SZ.INTEGER,
primaryKey: true,
autoIncrement: true
},
id_promotion_type: {
type: SZ.INTEGER,
allowNull: false
},
name: SZ.STRING,
}, {
sequelize,
tableName: 'promotions',
modelName: 'promotion',
})



class PromotionType extends SZ.Model {}

PromotionType.init({
id: {
type: SZ.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: SZ.STRING
}, {
sequelize,
tableName: 'promotion_types',
modelName: 'promotion_type',
})



Promotion.belongsTo(PromotionType, { foreignKey: 'id_promotion_type' })

Promotion.findAll({
include: {
model: PromotionType,
where: sequelize.where(sequelize.fn('unaccent_string', sequelize.col('name')), 'ilike', '%à%'),
}
})
.then(result => console.log(result))


你期望发生什么?

查询构建器应该认识到我所指的名称列是来自 PromotionType 模型的列,类似于 "promotion_type"."name"
实际发生了什么?

我收到错误,因为两个表中都存在名称

Unhandled rejection SequelizeDatabaseError: reference to column "name" is ambiguous

正在生成的查询:

SELECT "promotion"."id", 
"promotion"."id_promotion_type",
"promotion"."name",
"promotion"."createdAt",
"promotion"."updatedAt",
"promotion_type"."id" AS "promotion_type.id",
"promotion_type"."name" AS "promotion_type.name",
"promotion_type"."createdAt" AS "promotion_type.createdAt",
"promotion_type"."updatedAt" AS "promotion_type.updatedAt"
FROM "promotions" AS "promotion"
INNER JOIN "promotion_types" AS "promotion_type"
ON "promotion"."id_promotion_type" = "promotion_type"."id"
AND unaccent_string("name") ilike '%à%';

你们有什么解决办法吗?
谢谢关注。。

最佳答案

在 where 子句中添加表名 sequelize.col('promotion_type.name')

关于sequelize.js - 使用 sequelize.where 重复列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60840167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com