0
有沒有方法通過適用於其關聯字段的自己的字段或的條件查找模型?將模型的條件與其關聯的條件組合在Sequelize中
鑑於模型Model
和Association
,其中每個Model
有一個Association
。
const Model = sequelize.define('model', {
name: sequelize.STRING,
});
const Association = sequelize.define('association', {
name: sequelize.STRING,
});
Association.belongsTo(Model);
Model.hasOne(Association);
我想找到所有Model
S,要麼有一個name
等於「文本」,或有一個name
等於「文本」的Association
。
到目前爲止,我想出瞭解決方案Sequelize.literal
,這看起來不夠健壯。
Model.findAll({
attributes: ['id', 'name'],
include: [{
model: Association,
attributes: [],
}],
where: {
$or: [
{ name: 'test' },
Sequelize.literal('association.name = \'test\''),
],
},
});
有沒有更好的方法?