gpt4 book ai didi

sequelize.js - Sequelize 嵌套的急切加载不会加载与同一模型关联的两个记录

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

我正在尝试让 api 路由返回一个大对象,并在 sequelize 中嵌套急切加载。我的“车道”模型与“位置”模型有两个关联,分别是“origin_location_id”和“destination_location_id”。我试图同时返回“origin_location_id”和“destination_location_id”记录,但我只得到“destination_location_id”记录。

我尝试反转模型定义中的关联,模型定义中使用“as”和“through”以及查询中的多种语法变体,我继续要么没有得到服务器的响应,一个不描述的(甚至不是一个错误代码或错误描述)sequelizeeager loading 错误,或者我在json结果中只看到一个Location记录加载。

这是查询:

(function(req,res) {
models.Trip.findAll({
attributes: ['id', 'createdAt'],
include: [
{ model: models.Customer, attributes: ['id', 'name', 'email', 'address', 'phone'] },
{ model: models.Move, attributes: ['id', 'trip_id'], include: [
{ model: models.Lane, attributes: ['origin_location_id', 'destination_location_id', 'duration', 'distance'], include: [
{ model: models.Location, attributes: ['id', 'tookan_id', 'name', 'address', 'email', 'phone'] }
] }
] }
],
order:[['createdAt', 'DESC']]}).then(trips => {return res.json(trips)}).catch(err => res.status(422).json(err))})

这是 Lane 模型定义:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Lane = sequelize.define('Lane', {
active: {
type: DataTypes.TINYINT,
defaultValue: true },
customer_id: DataTypes.INTEGER,
description: DataTypes.TEXT,
origin_location_id: DataTypes.INTEGER,
destination_location_id: DataTypes.INTEGER,
distance: DataTypes.INTEGER,
duration: DataTypes.INTEGER,
driver_base_pay: DataTypes.DECIMAL,
driver_return_pay: DataTypes.DECIMAL,
tolls: DataTypes.DECIMAL,
driver_pay_per_minute: DataTypes.DECIMAL,
driver_pay_per_kilometer: DataTypes.DECIMAL,
average_drive_speed: DataTypes.DECIMAL
}, {});
Lane.associate = function(models) {
models.Lane.belongsTo(models.Customer, {foreignKey: 'customer_id'});
models.Lane.belongsTo(models.Location, {foreignKey: 'origin_location_id'});
models.Lane.belongsTo(models.Location, {foreignKey: 'destination_location_id'});
models.Lane.hasMany(models.Move, {foreignKey: 'lane_id'});
};
return Lane;
};

这是位置模型定义:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Location = sequelize.define('Location', {
tookan_id : DataTypes.INTEGER,
active: {
type: DataTypes.TINYINT,
defaultValue: true },
customer_id: DataTypes.INTEGER,
name: DataTypes.TEXT,
address: DataTypes.TEXT,
email: DataTypes.TEXT,
phone: DataTypes.TEXT
}, {});
Location.associate = function(models) {
models.Location.belongsTo(models.Customer, {foreignKey: 'customer_id'});
models.Location.hasMany(models.Lane, {foreignKey: 'origin_location_id'});
models.Location.hasMany(models.Lane, {foreignKey: 'destination_location_id'});
};
return Location;
};

这是当前的 JSON 结果结构(每个 Lane 记录只返回一个 Location 记录,尽管它与两个字段的 Locations 相关联):
{
"id": 27,
"createdAt": "2018-09-20T12:30:32.000Z",
"Customer": {
"id": 1,
"name": "",
"email": "",
"address": "",
"phone": ""
},
"Moves": [{
"id": 29,
"trip_id": 27,
"Lane": {
"id": 4,
"origin_location_id": 3,
"destination_location_id": 1,
"duration": 1260,
"distance": 21082,
"driver_base_pay": "20",
"driver_return_pay": "3",
"driver_pay_per_kilometer": "1",
"average_drive_speed": "18",
"Location": {
"id": 1,
"tookan_id": null,
"name": "",
"address": "",
"email": "",
"phone": ""
}
}
},
{
"id": 26,
"trip_id": 27,
"Lane": {
"id": 3,
"origin_location_id": 1,
"destination_location_id": 3,
"duration": 1260,
"distance": 21082,
"driver_base_pay": "20",
"driver_return_pay": "3",
"driver_pay_per_kilometer": "1",
"average_drive_speed": "18",
"Location": {
"id": 3,
"tookan_id": null,
"name": "",
"address": "",
"email": "",
"phone": ""
}
}
}
]
}

这是我得到的错误:
       {
"name": "SequelizeEagerLoadingError"
}

当我尝试这个时:
    (function(req,res) {
models.Trip.findAll({
include: [
{ model: models.Customer },
{ model: models.Move, include: [
{ model: models.Lane, include: [
{ model: models.Location, as: 'origin_location_id' },
{ model: models.Location, as: 'destination_location_id'}
] }
] }
],
order:[['createdAt', 'DESC']]
}).then(trips => {return res.json(trips)})
.catch(err => res.status(422).json(err))
})

最佳答案

我做了过去一个小时一直在做的同样的事情,它试图将“as”语句添加到关联中并使它们对应于查询,突然间它没有明显的原因就起作用了一个小时前上类。我所做的是将 Lane--Location 关联更改为models.Lane.belongsTo(models.Location, { as: 'pickup', foreignKey: 'origin_location_id'}); models.Lane.belongsTo(models.Location, { as: 'delivery', foreignKey: 'destination_location_id'});然后更新查询以匹配{ model: models.Location, as: 'pickup', attributes: ['id', 'tookan_id', 'name', 'address', 'email', 'phone'] }, { model: models.Location, as: 'delivery', attributes: ['id', 'tookan_id', 'name', 'address', 'email', 'phone'] }

关于sequelize.js - Sequelize 嵌套的急切加载不会加载与同一模型关联的两个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594710/

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