Reference main column from subquery Sequelize
const customers = await this.repository.findAndCountAll({ where: condition, attributes: { // exclude: [‘user_id’, ‘account_id’, ‘deletedAt’], include: [ [ Sequelize.literal(`( SELECT SUM(amount) FROM transactions WHERE transactions.customer_id = customers.id and transaction_type=’PAYOUT’ )`), ‘totalDebit’ ] ] }, limit, offset, include: { association: ‘groups’, where: { id: groupId }, through: { attributes: [] }, attributes:[], required: true }, order:[[‘createdAt’, ‘DESC’]], […]
Sequelize TypeScript ManyToMany relationship with same Table
How to define Many To Many relationship with custom foreign keys? Foreign keys must targeting on field, which is not Primary Key. Many To Many relationship connection same Table.