【发布时间】:2020-09-16 14:28:00
【问题描述】:
我有这个 postgres RAW 查询,我想用 Sequelize 编写它。我该怎么做,因为我对在 Sequelize 中编写具有 JOINS 的查询不太了解。我制作了模型和关联。
这些是模型和关联。
TestParticipant.hasMany(ParticipantHistory, {
sourceKey: "id",
foreignKey: "participantId",
as: "paticipantStatuses"
})
ParticipantHistory.belongsTo(TestParticipant, {
foreignKey: "participantId",
as: "paticipantStatuses"
})
这是我想转换为 Sequelize 查询的原始查询
SELECT participant_histories.participant_id,
participant_histories.created_at,participant_histories.previous_status,
participant_histories.status,test_participants.test_type_id,test_participants.id,
test_participants.email,test_participants.scheduled_at,test_participants.valid_till,
test_participants.is_proctored
FROM test_participants
INNER JOIN participant_histories ON test_participants.id=participant_histories.participant_id
WHERE user_id='${userId}'
AND participant_histories.status='${activity}'
AND participant_histories.created_at>='${isoDate}'
【问题讨论】:
-
首先创建模型和关联并将它们全部添加到帖子中,以便我们了解
participant_histories和test_participants之间的关系。这样我们就可以向您推荐如何将这个原始 SQL 查询转换为 Sequelize 查询 -
我已经做到了
-
以上两个表的模型模式在哪里。不知道表之间的关系不会更容易
标签: sql node.js postgresql sequelize.js