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

标签 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”以及查询中的多种语法变体,我继续要么没有得到服务器的响应,一个不描述的(甚至不是一个错误代码或错误描述)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/

相关文章:

node.js - 如何在返回分页元数据和数据集的 graphql 中实现分页

.net - 通过 NHibernate 使用无状态 session 渴望获取曾孙集合

linq - NHibernate Linq 提供程序中的 Fetch 与 FetchMany

php - Laravel eager load 功能限制

node.js - TypeError : include. model.getTableName 不是函数

heroku - 将 sequelize-cli 应用程序部署到 heroku DATABASE_URL 问题

mysql - Sequelize 原始查询返回 TextRows 数组

javascript - Sequelize 中两个表之间存在多种类型的多对多关系

node.js - 在 Sequelize 中添加具有起始值的自动递增整数

node.js - Sequelize : 'findAll' in instance method getParticipants()?