admin管理员组文章数量:1415467
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
1 Answer
Reset to default 5You 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
本文标签: javascriptHow I can change name in including field in sequelizeStack Overflow
版权声明:本文标题:javascript - How I can change name in including field in sequelize? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745167048a2645745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论