mysql - 如何从 sequelize 中的多级关联中获取结果?

标签 mysql sequelize.js

我有 3 个模型国家、城市和办公室

它们是相互关联的。 办公室属于城市,城市属于国家。 现在我想要做的是获得特定国家/地区内的办公室。 我在下面尝试过,但它不起作用。

Office.findAll({
  where: {'$Country.id$': 1},
  include: [
    { 
      model: City,
      as: 'city',
      include: [{model: Country, as: 'country'}]
    }
  ]
});

国家

module.exports = (sequelize, DataTypes) => {
    let Country = sequelize.define('Country', {
        id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
        code: {type: DataTypes.STRING, allowNull: false, unique: true },
        name: DataTypes.STRING
    });
    Country.associate = (models) => {
        Country.hasMany(models.City, {as: 'cities'});
    };
    return Country;
}

城市

module.exports = (sequelize, DataTypes) => {
    let City = sequelize.define('City', {
        id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
        name: {type: DataTypes.STRING, allowNull: false, unique: true},
    });

    City.associate = (models) => {
        City.belongsTo(models.Country, {as: 'country'});
        City.hasMany(models.Office, {as: 'offices'});
    };

    return City;
}

办公室

module.exports = (sequelize, DataTypes) => {
  let Office= sequelize.define('Office', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    name: DataTypes.STRING,
    details: DataTypes.TEXT,
    latitude: DataTypes.DECIMAL(10, 8),
    longitude: DataTypes.DECIMAL(11, 8),
  });

  Office.associate = (models) => {
    Office.belongsTo(models.City, {as: 'city'});
  };

  return Office;
};

最佳答案

你可以像这样包含所有相关的嵌套模型

const where = {
    'city.country.id': 1
};

Office.findAll({ where, include: [{ all: true, nested: true }]});

它会导致

SELECT
    `Office`.`id`,
    `Office`.`name`,
    `Office`.`details`,
    `Office`.`latitude`,
    `Office`.`longitude`,
    `Office`.`createdAt`,
    `Office`.`updatedAt`,
    `Office`.`cityId`,
    `Office`.`CityId`,
    `city`.`id` AS `city.id`,
    `city`.`name` AS `city.name`,
    `city`.`createdAt` AS `city.createdAt`,
    `city`.`updatedAt` AS `city.updatedAt`,
    `city`.`CountryId` AS `city.CountryId`,
    `city`.`countryId` AS `city.countryId`,
    `city->country`.`id` AS `city.country.id`,
    `city->country`.`code` AS `city.country.code`,
    `city->country`.`name` AS `city.country.name`,
    `city->country`.`createdAt` AS `city.country.createdAt`,
    `city->country`.`updatedAt` AS `city.country.updatedAt`
FROM
    `Offices` AS `Office`
    LEFT OUTER JOIN `Cities` AS `city` ON `Office`.`cityId` = `city`.`id`
    LEFT OUTER JOIN `Countries` AS `city->country` ON `city`.`countryId` = `city->country`.`id`
WHERE
    `Office`.`city.country.id` = 1;

关于mysql - 如何从 sequelize 中的多级关联中获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965298/

相关文章:

mysql - 如何让 mysql CASE 或 IF 跳过一条记录?

php - Dreamweaver CS6 中的 INNER JOIN 查询仅选择第一条记录

javascript - 模型中的函数接收消息 ** 不是函数 **

php - 使用 95% 标签的图像无法正常工作

mysql - 尝试从模型创建表时出错

php - 在 PHP 数组中显示多行

node.js - Sequelize -自动 : Changing Postgres Table Schemas

node.js - 在nodejs中保存日期时Sequelize出错

node.js - 将 Sequelize 模型转换为 JSON Schema 以进行用户输入验证

postgresql - 如何在 sequelize PostgreSQL 中根据用户 ID 对数据进行分组?