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

cuxqih21  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(382)

我有三个模型国家,城市和办公室
它们是相互联系的。办公室属于城市,城市属于国家。现在我要做的是在特定国家设立办事处。我在下面试过,但不起作用。

  1. Office.findAll({
  2. where: {'$Country.id$': 1},
  3. include: [
  4. {
  5. model: City,
  6. as: 'city',
  7. include: [{model: Country, as: 'country'}]
  8. }
  9. ]
  10. });

国家

  1. module.exports = (sequelize, DataTypes) => {
  2. let Country = sequelize.define('Country', {
  3. id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
  4. code: {type: DataTypes.STRING, allowNull: false, unique: true },
  5. name: DataTypes.STRING
  6. });
  7. Country.associate = (models) => {
  8. Country.hasMany(models.City, {as: 'cities'});
  9. };
  10. return Country;
  11. }

城市

  1. module.exports = (sequelize, DataTypes) => {
  2. let City = sequelize.define('City', {
  3. id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
  4. name: {type: DataTypes.STRING, allowNull: false, unique: true},
  5. });
  6. City.associate = (models) => {
  7. City.belongsTo(models.Country, {as: 'country'});
  8. City.hasMany(models.Office, {as: 'offices'});
  9. };
  10. return City;
  11. }

办公室

  1. module.exports = (sequelize, DataTypes) => {
  2. let Office= sequelize.define('Office', {
  3. id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
  4. name: DataTypes.STRING,
  5. details: DataTypes.TEXT,
  6. latitude: DataTypes.DECIMAL(10, 8),
  7. longitude: DataTypes.DECIMAL(11, 8),
  8. });
  9. Office.associate = (models) => {
  10. Office.belongsTo(models.City, {as: 'city'});
  11. };
  12. return Office;
  13. };
2hh7jdfx

2hh7jdfx1#

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

  1. const where = {
  2. 'city.country.id': 1
  3. };
  4. Office.findAll({ where, include: [{ all: true, nested: true }]});

它会导致

  1. SELECT
  2. `Office`.`id`,
  3. `Office`.`name`,
  4. `Office`.`details`,
  5. `Office`.`latitude`,
  6. `Office`.`longitude`,
  7. `Office`.`createdAt`,
  8. `Office`.`updatedAt`,
  9. `Office`.`cityId`,
  10. `Office`.`CityId`,
  11. `city`.`id` AS `city.id`,
  12. `city`.`name` AS `city.name`,
  13. `city`.`createdAt` AS `city.createdAt`,
  14. `city`.`updatedAt` AS `city.updatedAt`,
  15. `city`.`CountryId` AS `city.CountryId`,
  16. `city`.`countryId` AS `city.countryId`,
  17. `city->country`.`id` AS `city.country.id`,
  18. `city->country`.`code` AS `city.country.code`,
  19. `city->country`.`name` AS `city.country.name`,
  20. `city->country`.`createdAt` AS `city.country.createdAt`,
  21. `city->country`.`updatedAt` AS `city.country.updatedAt`
  22. FROM
  23. `Offices` AS `Office`
  24. LEFT OUTER JOIN `Cities` AS `city` ON `Office`.`cityId` = `city`.`id`
  25. LEFT OUTER JOIN `Countries` AS `city->country` ON `city`.`countryId` = `city->country`.`id`
  26. WHERE
  27. `Office`.`city.country.id` = 1;
展开查看全部
p4rjhz4m

p4rjhz4m2#

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

  1. Office.findAll({
  2. include: [
  3. {
  4. model: City,
  5. as: 'city',
  6. required : true , <----- Make sure will create inner join
  7. include: [
  8. {
  9. model: Country,
  10. as: 'country' ,
  11. required : true , // <----- Make sure will create inner join
  12. where : { 'id' : 1 } // <-------- Here
  13. }]
  14. }
  15. ]
  16. });
展开查看全部

相关问题