Answer the question
In order to leave comments, you need to log in
Sequelize to return calculated fields?
Tell me please. How to correctly make such a query in Sequelize (PostgreSQL)
1) several fields need to be contacted id || ' ' || id as somefield
2) one field rename code as codeCartidge,
select id || ' ' || id as somefield, code as codeCartidge, quantity, printed from "Cartridges"
const cartridges = await models.Cartridge.findAll({
order: ['code'],
attributes: [models.sequelize.literal("id || ' ' || id"), models.sequelize.literal('code AS codeCartridge'), 'quantity', 'printed']
});
[
{
"quantity": 500,
"printed": 0
},
{
"quantity": 500,
"printed": 0
},
...
Executing (default): SELECT id || ' ' || id, code AS codeCartridge, "quantity", "printed" FROM "Cartridges" AS "Cartridge" ORDER BY "Cartridge"."code"; { plain: false,
raw: false,
logging: [Function: bound consoleCall],
order: [ 'code' ],
attributes:
[ Literal { val: 'id || \' \' || id' },
Literal { val: 'code AS codeCartridge' },
'quantity',
'printed' ],
hooks: true,
rejectOnEmpty: false,
originalAttributes:
[ Literal { val: 'id || \' \' || id' },
Literal { val: 'code AS codeCartridge' },
'quantity',
'printed' ],
tableNames: [ 'Cartridges' ],
type: 'SELECT',
model: Cartridge }
Answer the question
In order to leave comments, you need to log in
It seems that I found a solution, for aliases you need to make a nested array
const cartridges = await models.Cartridge.findAll({
order: ['code'],
attributes: [[models.sequelize.literal("id || ' ' || id"), 'id'],
['code', 'codeCartridge'],
'quantity', 'printed']
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question