Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

677
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda