'Sequelize.fn("COUNT") in a query
So, I have a huge query where I need to count one of the attributes. The thing is that even though I tried all solutions out there, I am still getting the same problem.
This is part of the code. I have to get the count of the table "channel_subscribers" and I do it as the docs say
{
model: ChannelModel,
as: 'channels_created',
attributes: {
exclude: ['user_id']
},
include: [{
// required: true,
// duplicating: false,
// TODO: make this shit work
// docs: https://sequelize.org/master/manual/model-querying-basics.html
model: ChannelSubscriberModel,
as: 'channel_subscribers',
attributes: [
[
[[Sequelize.fn('COUNT', Sequelize.col('channel_subscribers.id')), 'subscribers_count']]
]
]
}]
},
You can see required: true and duplicating: false are commented bc as I saw on a GitHub issue people solved it that way. Wont work for me.
If I run the code as it is now I am getting the error s.replace is not a function
If I run the code with the line using only one pair of brackets ( [ ] )
[Sequelize.fn('COUNT', Sequelize.col('channel_subscribers.id')), 'subscribers_count'] I get the error missing FROM-clause entry for table \"channel_subscribers\"
Btw, I am using Postgresql
Here it is the query done by sequelize to the db
Executing (default): SELECT "users".*, "role"."id" AS "role.id", "role"."name" AS "role.name", "role"."description" AS "role.description", "detail"."id" AS "detail.id", "detail"."first_name" AS "detail.first_name", "detail"."last_name" AS "detail.last_name", "detail"."gender" AS "detail.gender", "detail"."url_picture" AS "detail.url_picture", "detail"."birthday" AS "detail.birthday", "detail"."bio" AS "detail.bio", "detail->country"."id" AS "detail.country.id", "detail->country"."name" AS "detail.country.name", "detail->country"."code" AS "detail.country.code", "payments"."id" AS "payments.id", "payments"."customer_id" AS "payments.customer_id", "payments"."start_date" AS "payments.start_date", "payments"."expiration_date" AS "payments.expiration_date", "payments"."subscription_status" AS "payments.subscription_status", "payments"."createdAt" AS "payments.createdAt", "payments"."updatedAt" AS "payments.updatedAt", "payments->subscription"."id" AS "payments.subscription.id", "payments->subscription"."name" AS "payments.subscription.name", "payments->subscription"."description" AS "payments.subscription.description", "followers"."id" AS "followers.id", "followers"."createdAt" AS "followers.createdAt", "followers"."updatedAt" AS "followers.updatedAt", "followers->follower_profile"."id" AS "followers.follower_profile.id", "followers->follower_profile"."uuid" AS "followers.follower_profile.uuid", "followers->follower_profile"."user_name" AS "followers.follower_profile.user_name", "followers->follower_profile"."email" AS "followers.follower_profile.email", "followers->follower_profile->detail"."id" AS "followers.follower_profile.detail.id", "followers->follower_profile->detail"."url_picture" AS "followers.follower_profile.detail.url_picture", "following"."id" AS "following.id", "following"."createdAt" AS "following.createdAt", "following"."updatedAt" AS "following.updatedAt", "following->user_profile"."id" AS "following.user_profile.id", "following->user_profile"."uuid" AS "following.user_profile.uuid", "following->user_profile"."user_name" AS "following.user_profile.user_name", "following->user_profile"."email" AS "following.user_profile.email", "following->user_profile->detail"."id" AS "following.user_profile.detail.id", "following->user_profile->detail"."url_picture" AS "following.user_profile.detail.url_picture", "channels_created"."id" AS "channels_created.id", "channels_created"."name" AS "channels_created.name", "channels_created"."private" AS "channels_created.private", "channels_created"."url_image" AS "channels_created.url_image", "channels_created"."createdAt" AS "channels_created.createdAt", "channels_created"."updatedAt" AS "channels_created.updatedAt", "channels_created->channel_subscribers"."id" AS "channels_created.channel_subscribers.id", COUNT("channel_subscribers"."id") AS "channels_created.channel_subscribers.subscribers_count", "channels_collaboration"."id" AS "channels_collaboration.id", "channels_collaboration->channel"."id" AS "channels_collaboration.channel.id", "channels_collaboration->channel"."name" AS "channels_collaboration.channel.name", "channels_collaboration->channel"."private" AS "channels_collaboration.channel.private", "channels_collaboration->channel"."url_image" AS "channels_collaboration.channel.url_image", "channels_collaboration->channel"."createdAt" AS "channels_collaboration.channel.createdAt", "channels_collaboration->channel"."updatedAt" AS "channels_collaboration.channel.updatedAt", "channel_subscriptions"."id" AS "channel_subscriptions.id", "channel_subscriptions"."createdAt" AS "channel_subscriptions.createdAt", "channel_subscriptions->channel"."id" AS "channel_subscriptions.channel.id", "channel_subscriptions->channel"."name" AS "channel_subscriptions.channel.name", "channel_subscriptions->channel"."private" AS "channel_subscriptions.channel.private", "channel_subscriptions->channel"."url_image" AS "channel_subscriptions.channel.url_image", "channel_subscriptions->channel"."createdAt" AS "channel_subscriptions.channel.createdAt", "channel_subscriptions->channel"."updatedAt" AS "channel_subscriptions.channel.updatedAt", "channel_subscriptions->channel->channel_subscribers"."id" AS "channel_subscriptions.channel.channel_subscribers.id", "channel_subscriptions->channel->channel_subscribers->user"."id" AS "channel_subscriptions.channel.channel_subscribers.user.id", "channel_subscriptions->channel->channel_subscribers->user"."user_name" AS "channel_subscriptions.channel.channel_subscribers.user.user_name" FROM (SELECT "users"."id", "users"."uuid", "users"."user_name", "users"."email", "users"."status", "users"."checked_email", "users"."createdAt", "users"."updatedAt", "users"."roles_id" FROM "security_v2"."users" AS "users" WHERE "users"."uuid" = 'c3d04015-cb71-47a9-b2e2-5806dfa171a9' LIMIT 1) AS "users" LEFT OUTER JOIN "security_v2"."roles" AS "role" ON "users"."roles_id" = "role"."id" LEFT OUTER JOIN "security_v2"."user_details" AS "detail" ON "users"."id" = "detail"."users_id" LEFT OUTER JOIN "security_v2"."countries" AS "detail->country" ON "detail"."countries_id" = "detail->country"."id" LEFT OUTER JOIN "security_v2"."payments" AS "payments" ON "users"."id" = "payments"."users_id" LEFT OUTER JOIN "security_v2"."subscription_types" AS "payments->subscription" ON "payments"."subscription_types_id" = "payments->subscription"."id" LEFT OUTER JOIN "security_v2"."user_followers" AS "followers" ON "users"."id" = "followers"."user_id" LEFT OUTER JOIN "security_v2"."users" AS "followers->follower_profile" ON "followers"."follower_id" = "followers->follower_profile"."id" LEFT OUTER JOIN "security_v2"."user_details" AS "followers->follower_profile->detail" ON "followers->follower_profile"."id" = "followers->follower_profile->detail"."users_id" LEFT OUTER JOIN "security_v2"."user_followers" AS "following" ON "users"."id" = "following"."follower_id" LEFT OUTER JOIN "security_v2"."users" AS "following->user_profile" ON "following"."user_id" = "following->user_profile"."id" LEFT OUTER JOIN "security_v2"."user_details" AS "following->user_profile->detail" ON "following->user_profile"."id" = "following->user_profile->detail"."users_id" LEFT OUTER JOIN "security_v2"."channels" AS "channels_created" ON "users"."id" = "channels_created"."user_id" LEFT OUTER JOIN "security_v2"."channel_subscribers" AS "channels_created->channel_subscribers" ON "channels_created"."id" = "channels_created->channel_subscribers"."channel_id" LEFT OUTER JOIN "security_v2"."channel_collaborators" AS "channels_collaboration" ON "users"."id" = "channels_collaboration"."user_id" LEFT OUTER JOIN "security_v2"."channels" AS "channels_collaboration->channel" ON "channels_collaboration"."channel_id" = "channels_collaboration->channel"."id" LEFT OUTER JOIN "security_v2"."channel_subscribers" AS "channel_subscriptions" ON "users"."id" = "channel_subscriptions"."user_id" LEFT OUTER JOIN "security_v2"."channels" AS "channel_subscriptions->channel" ON "channel_subscriptions"."channel_id" = "channel_subscriptions->channel"."id" LEFT OUTER JOIN "security_v2"."channel_subscribers" AS "channel_subscriptions->channel->channel_subscribers" ON "channel_subscriptions->channel"."id" = "channel_subscriptions->channel->channel_subscribers"."channel_id" LEFT OUTER JOIN "security_v2"."users" AS "channel_subscriptions->channel->channel_subscribers->user" ON "channel_subscriptions->channel->channel_subscribers"."user_id" = "channel_subscriptions->channel->channel_subscribers->user"."id";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
