Sequelize set alias attributes name after join

12,354

Write query like this:

const result = await Table.findAll({
  attributes: ['id', ['foo', 'bar']] //id, foo AS bar
});
Share:
12,354

Related videos on Youtube

gio
Author by

gio

Updated on June 04, 2022

Comments

  • gio
    gio almost 2 years

    After a join operation among three models I received a valid result but I would rename the attributes generated by the join operation of the findAll

    Query:

    const orchards = await db.Area.findAll({
            include: [db.AreaCoordinate, db.Crop],
            attributes: ['id', 'name']
          });
    

    AreaCoordinate Model:

    module.exports = function (sequelize, DataTypes) {
    
      var AreaCoordinate = sequelize.define('AreaCoordinate', {
        latitude: {
          type: DataTypes.STRING(45),
          allowNull: true
        },
        longitude: {
          type: DataTypes.STRING(45),
          allowNull: true
        }
      }, {
        classMethods: {
          associate: function (models) {
            AreaCoordinate.belongsTo(models.Area, {foreignKey: 'areaId'});
          }
        }
      });
    
      return AreaCoordinate;
    };
    

    Crop Model:

    module.exports = function (sequelize, DataTypes) {
    
      var Crop = sequelize.define('Crop', {
        name: {
          type: DataTypes.STRING(45),
          allowNull: true
        },
        lang: {
          type: DataTypes.STRING(45),
          allowNull: true
        }
      }, {
        classMethods: {
          associate: function (models) {
            Crop.hasMany(models.Area, {foreignKey:'cropId'})
          }
        }
      });
    
      return Crop;
    };
    

    Area Model:

    module.exports = function (sequelize, DataTypes) {
    
      var Area = sequelize.define('Area', {
        name: DataTypes.STRING
      }, {
        classMethods: {
          associate: function (models) {
            // example on how to add relations
            Area.belongsTo(models.Crop, {foreignKey: 'cropId'});
            Area.belongsTo(models.Orchard, {as: 'orchard'});
            Area.hasMany(models.AreaCoordinate, {foreignKey:'areaId'})
          }
        }
      });
    
      return Area;
    };
    

    I would receive from the query a JSON like this:

    {
        "status": 200,
        "status_message": "OK",
        "data": {
            "orchard": [
                {
                    "name": "pantano",
                    "coordinates": [
                        {
                            "id": 115,
                            "latitude": "1",
                            "longitude": "2",
                            "createdAt": "2017-08-29T12:03:11.000Z",
                            "updatedAt": "2017-08-29T12:03:11.000Z",
                            "areaId": 28
                        },
                        {
                            "id": 116,
                            "latitude": "1",
                            "longitude": "2",
                            "createdAt": "2017-08-29T12:03:11.000Z",
                            "updatedAt": "2017-08-29T12:03:11.000Z",
                            "areaId": 28
                        }
                    ],
                    "cropId": 10
                }
            ]
        }
    }
    

    But what I receive is (look AreaCoordinates and Crop):

    {
        "status": 200,
        "status_message": "OK",
        "data": {
            "orchard": [
                {
                    "name": "pantano",
                    "AreaCoordinates": [
                        {
                            "id": 115,
                            "latitude": "1",
                            "longitude": "2",
                            "createdAt": "2017-08-29T12:03:11.000Z",
                            "updatedAt": "2017-08-29T12:03:11.000Z",
                            "areaId": 28
                        },
                        {
                            "id": 116,
                            "latitude": "1",
                            "longitude": "2",
                            "createdAt": "2017-08-29T12:03:11.000Z",
                            "updatedAt": "2017-08-29T12:03:11.000Z",
                            "areaId": 28
                        }
                    ],
                    "Crop": 10
                }
            ]
        }
    }
    

    I tried to set some alias for AreaCoordinates and Crop but I couldn't find a solution. Thank you in advance for your support.