gpt4 book ai didi

mysql - 获取 Sequelize Association 生成的错误查询

转载 作者:行者123 更新时间:2023-11-29 15:16:15 25 4
gpt4 key购买 nike

我有两张 table 。第一个是 Order,第二个是 OrderStatus
在订单表中,order_status_codeforeignKey,它引用 OrderStatus 表的 id
我有以下模型关联定义。

 Order.associate = function(models) {
// associations can be defined here
Order.hasOne(models.OrderItem,{foreignKey: "order_id"}),
Order.hasOne(models.OrderStatus, {foreignKey: "order_status_code"})
};

我收到以下错误:

Unknown column 'OrderStatus.order_status_code' in 'field list

当我尝试立即加载 OrderStatus 时。

const orders = await Order.findAll({
where: filter,
include: {
model: OrderStatus
}
})

下面是控制台上显示的查询。

SELECT `Order`.`id`, `Order`.`buyer_id`, `Order`.`order_status_code`, `Order`.`order_detail`, `Order`.`order_date`, `Order`.`order_number`, `Order`.`created_at`, `Order`.`updated_at`, `OrderStatus`.`id` AS `OrderStatus.id`, `OrderStatus`.`order_status_code` AS `OrderStatus.order_status_code`, `OrderStatus`.`status` AS `OrderStatus.status`, `OrderStatus`.`created_at` AS `OrderStatus.created_at`, `OrderStatus`.`updated_at` AS `OrderStatus.updated_at` FROM `Orders` AS `Order` LEFT OUTER JOIN `OrderStatuses` AS `OrderStatus` ON `Order`.`order_status_code` = `OrderStatus`.`id` WHERE `Order`.`buyer_id` = 23;

我不知道为什么选择OrderStatus.order_status_code

最佳答案

我通过定义属性来从包含的模型中进行选择来修复它,现在它解决了这个问题。

const orders = await Order.findAll({
where: filter,
include: {
model: OrderStatus,
attributes:["status"]
}
})

关于mysql - 获取 Sequelize Association 生成的错误查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59678451/

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