Quiero obtener de la cuenta de la tabla principal y compararla con el historial de la cuenta. hay una relación On To many entre las dos mesas
SELECT account."userName", (account."postNumber" - accountHistory."postNumber") AS postNumber FROM public."Accounts" AS account INNER JOIN public."AccountHistories" AS accountHistory ON account."userName" = accountHistory."userName" WHERE accountHistory."scrappingDate" = '2022-01-08 23:59:39+01' ORDER BY postNumber DESC;
Mi pregunta es como puedo hacer la consulta con sequilize? Lo intenté pero no pude cambiar el nombre del atributo y hacer la resta
await this.accountRepository.findAll({ where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'}, include: [{ model: AccountHistory, required: false }], attributes: ['userName',['$accountHistory.postNumber $', 'postNumber '],postNumber ], order: [[$accountHistory.createdAt$', 'DESC']], })
Probablemente necesite usar Sequelize.literal para realizar el cálculo como un atributo con alias, por ejemplo:
const { literal } = require('sequelize') await this.accountRepository.findAll({ where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'}, include: [{ model: AccountHistory, required: false }], attributes: [ 'userName', [ literal('"account"."postNumber" - "accountHistory"."postNumber"'), 'postNumber' ] ], order: [[$accountHistory.createdAt$', 'DESC']], })
Es posible que esa sintaxis exacta no funcione según su dialecto SQL; no estoy familiarizado con los símbolos $
que está usando