我编写了一个SQL查询,并尝试将其调整为Sequelize,但无法弄清楚如何在同一个表上创建关联。这是SQL:
"\n'OPHT'\n'GALE'\n'CEMP'\n'TKAI'\n'ANTH'\n'ADPT'\n\n\n'CYTR'\n'NVAX'\n'MRTX'\n'IMGN'\n'OVAS'\n'AVGR'\n'DVAX'\n'INFI'\n'TNDM'\n'FNBC'\n\n\'CVRS'\n'CLDX'\n'CIE'\n'ARWR'\n'CYH'\n'RGLS'\n'VSLR'\n'IMDZ'\n'ITCI'\n\n\n\n\n'MDCA'
以下是模型定义:
SELECT t1.*
FROM `configurations` t1
INNER JOIN
(
SELECT max(configuration_start) maxConfigStart, pb_id
FROM `configurations`
WHERE
active = 1
AND DATE(configuration_start) <= DATE(NOW())
GROUP BY `pb_id`
) t2
ON t1.`pb_id` = t2.`pb_id`
AND t1.`configuration_start` = t2.`maxConfigStart`
WHERE t1.`active` = 1
ORDER BY t1.`configuration_start` DESC
非常感谢任何人提供的任何帮助。基本上我正在尝试为每个configurations = sequelize.define 'configurations', {
id:
type: Sequelize.INTEGER
primaryKey: true
autoIncrement: true
configuration_name: Sequelize.STRING
configuration_start: Sequelize.DATE
configuration_end: Sequelize.DATE
created: Sequelize.DATE
updated: Sequelize.DATE
}, {
timestamps: false,
scopes: {
currentConfigTimes: {
attributes: ['pb_id', [sequelize.fn('max', sequelize.col('configuration_start')), 'configuration_start']]
where:
active: 1
configuration_start:
lte: new Date()
group: ['pb_id']
},
currentConfigIds: {
attributes: ['pb_id', 'configuration_start', 'id']
where:
active: 1
configuration_start:
lte: new Date()
order: [['configuration_start', 'DESC']]
}
}
}
获取最新的过去配置。也许还有更好的方法吗?
答案 0 :(得分:0)
目前看来,使用Sequelize实际上是不可能的。在花了很多时间用他们的文档试图理解这样做的最佳方式后,我找到this Github issue试图解决完全相同的问题。最好的解决方案是在此时使用原始查询。