I am trying to order by the count of the associated model, and have been getting the following error from Sequelize
Cannot read property '_modelAttribute' of undefined
I have following code:
db.user.belongsToMany(db.user, {
as: "User",
through: "UserFollowers",
foreignKey: "user_id"
});
db.user.belongsToMany(db.user, {
as: "Follower",
through: "UserFollowers",
foreignKey: "follower_id"
});
and
User.findAll({
attributes:["username",db.sequelize.literal('(SELECT COUNT(*) FROM "public"."Users","public"."UserFollowers" WHERE "Users".user_id = "UserFollowers".user_id GROUP BY "Users".user_id)'),'followercount'],
include:[{model:User,as:'Follower'}],
order:[{ model: User, as: 'Follower' }[db.sequelize.literal('followercount')],'DESC']
}).then(users => {
res.json(users)
})
I appreciate any help!
I am trying to order by the count of the associated model, and have been getting the following error from Sequelize
Cannot read property '_modelAttribute' of undefined
I have following code:
db.user.belongsToMany(db.user, {
as: "User",
through: "UserFollowers",
foreignKey: "user_id"
});
db.user.belongsToMany(db.user, {
as: "Follower",
through: "UserFollowers",
foreignKey: "follower_id"
});
and
User.findAll({
attributes:["username",db.sequelize.literal('(SELECT COUNT(*) FROM "public"."Users","public"."UserFollowers" WHERE "Users".user_id = "UserFollowers".user_id GROUP BY "Users".user_id)'),'followercount'],
include:[{model:User,as:'Follower'}],
order:[{ model: User, as: 'Follower' }[db.sequelize.literal('followercount')],'DESC']
}).then(users => {
res.json(users)
})
I appreciate any help!
Share Improve this question asked Feb 1, 2019 at 9:38 TripleNTripleN 311 silver badge4 bronze badges1 Answer
Reset to default 6From glancing at the code, the issue seems to be on the ordering clause. Change it to:
order: [[{ model: User, as: 'Follower' }, 'followercount', 'DESC']]
If that doesn't cut it, try logging the oute of the query by adding logging: console.log
. That should help you identify what part is failing by analyzing the sql query.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744397723a4572230.html
评论列表(0条)