Im a noob in Sequelize.js and somewhat less in Angular but must solve a problem in which I want to use a subquery as a condition of a JOIN. I paste some examples below because code says more then words.
SQL
INNER JOIN table ON table.key IN(SELECT current_key FROM historysnake WHERE original_key IN(
SELECT original_key FROM historysnake WHERE current_key = table.key)
AND historysnake.model = 'tablename')
The question is: How can I put above query into a Sequelize object? Something like:
Sequelize.js
var foo = sequelize.define('foo', {...}, {
classMethods: {
associate: function(models) {
foo.hasMany(...)
}
}
});
Ok, here's an example, which assumes that PK of PatientEvents is the original_key on all the history rows:
PatientEvents.hasMany(HistorySnake, {foreignKey : 'original_key'});
PatientEvents.findAll({
include: [{
model: HistorySnake,
required : true, // true = INNER JOIN but you might need outer join to see events with no history
where : { modelName : 'PatientEvents' } // as needed
}],
where: { patient_name : 'xyz' } // as needed
})
I figured it out. I'm not really sure if @KenOn10's answer would've worked. I'm too much of a noob on this subject for that but thanks for the answer anyway.
I ended up specifying my own 'on' clause like this.
models.Aim.find({
include: [{
model: models.ObjectiveReport,
required: false,
where: ['ObjectiveReports.report_entrydate >= ?', show_date],
on: {
aim_id: sequelize.literal('aim.aim_id IN(SELECT current_key FROM historysnake WHERE original_key IN(SELECT original_key FROM historysnake WHERE current_key = aim.aim_id) AND historysnake.model = "aim")')
})
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.