-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Closed
Description
What you are doing?
There's a 1:n association between model A and model B. I'd like to find one A entity with all associated B entities. I only want to get a few selected attributes. All fields with a virtual getter are sent with the included model. The attributes don't seem to be respected for fields with a getter.
models.A.findOne({
where: {id: 5},
attributes: ['id'],
include: [{
model: models.B,
attributes: ['id']
}]
})
.then(function(r) {
...
})
var A = sequelize.define("A", {
text: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
A.hasMany(models.B, {foreignKey: 'a_id'})
}
}
});
var B = sequelize.define("B", {
text: {
type: DataTypes.STRING,
get: function() {
return this._toArr(this.getDataValue('text'))
},
priority: DataTypes.INTEGER(1).UNSIGNED
}
}, {
classMethods: {
associate: function(models) {
B.belongsTo(models.A, {foreignKey: 'a_id', foreignKeyConstraint: true})
}
},
instanceMethods: {
// convert string to array
_toArr: function(s) {
if(s) {
var arr = _.split(s, ';')
return arr
}
return []
}
}
});
What do you expect to happen?
{ id: 5,
B:
[ { id: 1 },
{ id: 2 } ]
}
What is actually happening?
{ id: 5,
B:
[ { id: 1, text: [...] },
{ id: 2, text: [...] } ]
}
Metadata
Metadata
Assignees
Labels
No labels