我是sequelize
的新手,我已经下达了将sequelize
原始查询更改为ORM 的任务,我对很多事情感到困惑。如果有人可以提供帮助,我将提供带有关联的示例查询,请让我摆脱这种痛苦。谢谢
db.sequelize.query('SELECT id, username, full_name, profile_image_url, ' +
'(SELECT COUNT(*) from public."Followships" WHERE public."Followships".leader_id = :user_id) AS followers, ' +
'(SELECT COUNT(*) from public."Followships" WHERE public."Followships".follower_id = :user_id) AS following, ' +
'(SELECT array_agg(interest_id) from public."UserInterestsMaps" WHERE public."UserInterestsMaps".user_id = :user_id) AS user_interests, ' +
'FROM public."Users" ' +
'where public."Users".id = :user_id', { replacements: { user_id: req.params.user_id }, type: db.sequelize.QueryTypes.SELECT })
奖学金与用户表的关联
Followship.belongsTo(models.User, {
as: 'followings',
foreignKey: 'leader_id',
foreignKeyConstraint: true
});
Followship.belongsTo(models.User, {
as: 'followers',
foreignKey: 'follower_id',
foreignKeyConstraint: true
});
我正试图了解sequelize
,但这不是那么简单。