javascript - How I can change name in including field in sequelize? - Stack Overflow

How I can change name in request object, if I use sequelize.I have two models, and Person model includ

How I can change name in request object, if I use sequelize. I have two models, and Person model include Name model.

Table in db is Names, and my field name is Names. How I can . change it?

Person.findByPk(
    id,
    include: [{
        model: Name
    }]
});

Name.Model.js

sequelize.define('Name', {
    id: {
        type: type.INTEGER,
        primaryKey: true
    },
    name: {
        type: type.STRING
    }
});

and I get object Person, where one filed is also object

Person = {
   ...
   Names: {
      id: 1,
      name: Piter
   }
}

I want

Person = {
   ...
   name: {
      id: 1,
      name: Piter
   }
}

How I can change name in request object, if I use sequelize. I have two models, and Person model include Name model.

Table in db is Names, and my field name is Names. How I can . change it?

Person.findByPk(
    id,
    include: [{
        model: Name
    }]
});

Name.Model.js

sequelize.define('Name', {
    id: {
        type: type.INTEGER,
        primaryKey: true
    },
    name: {
        type: type.STRING
    }
});

and I get object Person, where one filed is also object

Person = {
   ...
   Names: {
      id: 1,
      name: Piter
   }
}

I want

Person = {
   ...
   name: {
      id: 1,
      name: Piter
   }
}
Share Improve this question asked May 21, 2019 at 21:08 user10053586user10053586 792 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You should be able to use the as property in your association wherever you defined it. For example your Person Model could look like this.

const Person = sequelize.define('Person', 
    { 
     .
     . //whatever other properties your person has.
     .
     },{});
Person.associate = function(models){
    Person.hasOne(models.Name, {
        as: 'name'
    });
};

Edit: More information regarding this subject can be found here: http://docs.sequelizejs./manual/associations.html#naming-strategy

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745167048a4614696.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信