Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

678
Views
sequelize : how rename column in include

I have this query :

    const data = await modelOwners.findAll({
  attributes: ["id", "name", "email"],
  include: [
    {
      model: modelLotsTantiemes,
      as: "LotsOwner",
      attributes: [
        "lotId",
        ["distributionKeyId", 'costKey'],
        [ Sequelize.fn('sum', Sequelize.col("tantiemeNumber")), 'totalTantieme' ],
      ],
    },
  ],
  where: { coproNumber: req.header("customerId")},
  group: ["name", "LotsOwner.distributionKeyId"],
  raw: true,
  nest: false,
  order: [["name", "ASC"]],
});

When I test it on Postman Y have this result :

"data": [
    {
        "id": 4,
        "name": "Clement Tine",
        "email": "clementt@fruitsetlegumes.fr",
        "LotsOwner.field1": 2,
        "LotsOwner.field2": 1,
        "LotsOwner.field3": "1892"
    },
    {
        "id": 6,
        "name": "Léo Pard",
        "email": "leopard@thoiry.fr",
        "LotsOwner.field1": 9,
        "LotsOwner.field2": 1,
        "LotsOwner.field3": "4432"
    }]

But I would like an other name for my columns like :

"data": [
    {
        "id": 4,
        "name": "Clement Tine",
        "email": "clementt@fruitsetlegumes.fr",
        "field1": 2,
        "field2": 1,
        "field3": "1892"
    }]
  

I don't want the name of the association alias. Do you know if there is an option to do it ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you wish to have attributes of an associated model on the same level as the main model then you need to include them manually indicating the associated model name:

const data = await modelOwners.findAll({
  attributes: ["id", "name", "email",
  [Sequelize.col('"LotsOwner"."distributionKeyId"'), 'costKey']
  ],
  include: [
    {
      model: modelLotsTantiemes,
      as: "LotsOwner",
...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!