【问题标题】:Sequelize where with includeSequelize where with include
【发布时间】:2016-02-05 06:54:45
【问题描述】:

我使用 sequelize 作为我的后端 ORM。 但是当我想要连接表的“位置”时,我遇到了问题。联想很好,但我不知道如何为“哪里”做。

这是我的代码:

router.get('/id_presta_struct_unit/:id_presta_struct_unit', (req, res) => {
  models.structures.findAll({
    include: {
      required: false,
      model: models.structures_proposer_prestations,
      where: {
        id_presta_struct_unit: req.params.id_presta_struct_unit
      },
      include: {
        model : models.unites_facturation,
      }
    }
  }).then(data => {
    res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
    res.end(JSON.stringify(data));
  });
});

我收到了这个请求

SELECT * FROM structures AS structures LEFT OUTER JOIN structures_proposer_prestations AS structures_proposer_prestations ON structures.id_structure = structures_proposer_prestations.structures_proposer_prestations.id_structure AND @9876543333332@@. = '1' 左外连接 unites_facturation AS structures_proposer_prestations.unites_facturation ON structures_proposer_prestations.id_unite = structures_proposer_prestations.unites_facturation.id_unite;

但我想得到

SELECT * FROM structures AS structures LEFT OUTER JOIN structures_proposer_prestations AS structures_proposer_prestations ON structures.id_structure = structures_proposer_prestations.id_structure LEFT OUTER JOIN @987654 structures_proposer_prestations.id_unite = structures_proposer_prestations.unites_facturation.id_unite 在哪里structures_proposer_prestations.id_presta_struct_unit = '1';

我不知道该怎么办我没有找到有同样问题的帖子

谁能指出我正确的方向?

提前谢谢你。

编辑:

协会

models.structures_employer_ressources.hasMany(models.ressources, { foreignKey: 'id_ressource' });
models.ressources.belongsTo(models.structures_employer_ressources, { foreignKey: 'id_ressource' });

资源模型

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ressources', {
    id_ressource: {
      type: DataTypes.STRING,
      allowNull: false,
      primaryKey: true
    }
 ........
  },{
    tableName: 'ressources',
    updatedAt: 'date_modification',
    createdAt: 'date_creation'
  });
};

以及structures_employer_ressources的模型

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('structures_employer_ressources', {
    id_structure: {
      type: DataTypes.STRING,
      allowNull: false,
      primaryKey: true,
      references: {
        model :'structures',
        key: 'id_structure'
      }
    },
    id_ressource: {
      type: DataTypes.STRING,
      allowNull: false,
      primaryKey: true,
      references: {
        model :'ressources',
        key: 'id_ressource'
      }
    }
  },{
    tableName: 'structures_employer_ressources',
    updatedAt: 'date_modification',
    createdAt: 'date_creation'
  });
};

【问题讨论】:

标签: mysql node.js join where sequelize.js


【解决方案1】:

如果将数组输入到初始连接的 where 子句中,则可以对连接运行原始查询。

例子:

 models.structures.findAll({
where:[["[structures_proposer_prestations].[id_presta_struct_unit] = " + req.params.id_presta_struct_unit, null]],
    include: {
      required: false,
      model: models.structures_proposer_prestations,
      where: {
        id_presta_struct_unit: req.params.id_presta_struct_unit
      },
      include: {
        model : models.unites_facturation,
      }
    }
  }

该数组也可以采用标准对象语法,并将由 AND 组合。我传入的null是用于参数的,所以绝对可以优化以将id作为参数,只是不知道语法。

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2016-12-17
    • 2018-09-24
    相关资源
    最近更新 更多