嵌套嵌套急切加载不会同时加载与同一模型关联的两条记录

时间:2018-10-01 15:51:17

标签: sequelize.js eager-loading sequelize-cli

我正在尝试让api路由返回一个大型对象,并在sequelize中嵌套嵌套。我的“车道”模型与“位置”模型有两个关联,分别是“ 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))})

这是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记录与两个字段的Locations关联,但仅返回一个Location记录): { "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))
    })

2 个答案:

答案 0 :(得分:0)

我做了过去一小时一直在做的事情,它试图将“ 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'] }

答案 1 :(得分:0)

我在使用模型关联进行预加载时遇到了类似的问题。我的问题是执行了内连接,而我确实需要外连接。

改了这个: Foo.findAll(include: [ Bar ])

为此: Foo.findAll(include: [ { model: Bar, required: false }])