John
John

Reputation: 353

Rename column in sequelize findAll

I have the following findAll query:

let consumQuery = Manager.consumption.findAndCountAll({
        where: query, attributes: ['ml', 'price', 'total', 'consumption_end', 'bar_id', 'sync', 'visit.device_id', 
                                    'visit.person.document','visit.person.person_id','visit.person.name','visit.person.surname',
                                    'keg.product_id','keg.product.sku','keg.product.name'],
        include: [
            {
                model: Manager.visit,
                attributes: [],
                include: [{
                    model: Manager.person,
                    attributes: [],
                    raw: true
                }],
                
            },
            {
                model: Manager.keg,
                attributes: [],
                include: [
                    {
                        model: Manager.product,
                        required: true,
                        attributes: []
                    }
                ]
            }],
            limit: PAGE_LIMIT_SIZE,
            offset: offset,
            raw: true,
        order: [['consumption_start', 'ASC']]
    }).then(({count, rows}) => {

I'm trying to rename the column keg.product.name to beer. I've tried :

...'keg.product_id','keg.product.sku',['keg.product.name','beer']

But I got the error:

"column \"keg.product.name\" does not exist"

The sequelize output the following SQL:

sql: `SELECT "consumption"."ml", "consumption"."price", "consumption"."total", "consumption"."consumption_end", "consumption"."bar_id", "consumption"."sync", "visit"."device_id", "visit.person"."document", "visit.person"."person_id", "visit.person"."name", "visit.person"."surname", "keg"."product_id", "keg.product"."sku", "keg.product.name" AS "beer" FROM "public"."consumption" AS "consumption" LEFT OUTER JOIN "public"."visit" AS "visit" ON "consumption"."visit_id" = "visit"."visit_id" LEFT OUTER JOIN "public"."person" AS "visit.person" ON "visit"."person_id" = "visit.person"."person_id" LEFT OUTER JOIN "public"."keg" AS "keg" ON "consumption"."keg_id" = "keg"."keg_id" INNER JOIN "public"."product" AS "keg.product" ON "keg"."product_id" = "keg.product"."product_id" WHERE ("consumption"."bar_id" = '248' AND "consumption"."sync" = 'true' AND "consumption"."consumption_start" >= '2020-02-16 20:03:32.000 -03:00' AND "consumption"."consumption_end" <= '2020-09-17 20:03:32.000 -03:00') ORDER BY "consumption"."consumption_start" ASC LIMIT 500 OFFSET 0;},

The result is "keg.product.name" AS "beer", but I think should be something like that: "keg.product"."name" AS "beer"

Any ideia?

Upvotes: 0

Views: 1197

Answers (1)

Jovane de Castro
Jovane de Castro

Reputation: 26

To rename an attribute you have to make it an array like [name_in_db, name_you_want].

Therefore your attributes value should be as it follows

attributes: ['ml', 'price', 'total', 'consumption_end', 'bar_id', 'sync', 'visit.device_id', visit.person.document', 'visit.person.person_id', 'visit.person.name', 'visit.person.surname', keg.product_id', 'keg.product.sku', **['keg.product.name', 'beer']**]

Source: https://sequelize.org/api/v6/class/src/model.js~model#static-method-findAll
"To rename an attribute, you can pass an array, with two elements - the first is the name of the attribute in the DB [...], and the second is the name you want the attribute to have in the returned instance"

Upvotes: 1

Related Questions