如何从多个级别关联中获得结果?

时间:2018-08-22 10:55:14

标签: mysql sequelize.js

我有3个国家,城市和办公室型号

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

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;
};

2 个答案:

答案 0 :(得分:3)

您可以直接从include从国家进行查询,并使用required : true,请尝试以下操作:

Office.findAll({
  include: [
    { 
      model: City,
      as: 'city',
      required : true , <----- Make sure will create inner join
      include: [
            {
                model: Country, 
                as: 'country' ,
                required : true , // <----- Make sure will create inner join
                where : { 'id' : 1 } // <-------- Here
            }]
    }
  ]
});

答案 1 :(得分:2)

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

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;