Sequelize在连接后设置别名属性名称

时间:2017-08-29 13:04:58

标签: mysql node.js express sequelize.js node-modules

在三个模型之间的连接操作之后,我收到了一个有效的结果,但我会重命名由findAll的连接操作生成的属性

查询:

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

AreaCoordinate模型:

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;
};

裁剪模型:

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;
};

区域型号:

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;
};

我会从查询中收到像这样的JSON:

{
    "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
            }
        ]
    }
}

但我得到的是(看看AreaCoordinates和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
            }
        ]
    }
}

我尝试为 AreaCoordinates 裁剪设置一些别名,但我无法找到解决方案。提前感谢您的支持。

2 个答案:

答案 0 :(得分:1)

这样写查询:

f1 <- function(vec) {
      if(all(nchar(vec)==5)) { 
        vec <- paste0("00:", vec)
       }
        vec %>%
           hms() %>%
           period_to_seconds()

}

f1(a)
#[1] 1 3 5
f1(b)
#[1] 1 3 5

答案 1 :(得分:0)

在Sequelize关联中,默认情况下,它将属性名称设置为相关的模型名称。对于您的情况,相关模型名为 AreaCoordinates ,因此返回的属性名称将为 AreaCoordinates 。您应使用 as 。修改您的内容并尝试以下操作:

findAll查询:

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

区域模型

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',
                as: 'cropId'
            });
            Area.belongsTo(models.Orchard, {as: 'orchard'});
            Area.hasMany(models.AreaCoordinate, {
                foreignKey:'areaId',
                as: 'coordinates'
            })
        }
    }
});

return Area;
};