【发布时间】: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