【发布时间】:2019-03-06 18:37:09
【问题描述】:
我正在尝试让 api 路由返回一个带有嵌套预加载的大对象。我的“车道”模型与“位置”模型有两个关联,即“origin_location_id”和“destination_location_id”。我正在尝试返回“origin_location_id”和“destination_location_id”记录,但我只得到“destination_location_id”记录。
我尝试在模型定义中反转关联,在模型定义中使用“as”和“through”以及在查询中使用多种语法变体,但我仍然没有得到服务器的响应,一个无法描述的 (甚至没有错误代码或错误描述)继续急切加载错误,或者我在 json 结果中只看到一个位置记录加载。
这是查询:
(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))})
这是车道模型定义:
'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;
};
这是 Location 模型定义:
'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”或“through”时,我都没有收到来自服务器的响应或 Sequelize Eager Loading 错误
-
发表于 github 问题的续集:github.com/sequelize/sequelize/issues/9976
标签: sequelize.js eager-loading sequelize-cli