Can't exclude association's fields from select statement in sequelize
See original GitHub issueI have the following code (simplified):
var group = sequelize.define("group", {
id: {type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true},
name: type: DataTypes.STRING,
parentId: DataTypes.INTEGER
}, { classMethods: {
associate: function (models) {
group.belongsToMany(models.item, { as:'items', foreignKey: 'group_id', through: models.group_item_tie });
}}
});
var group_item_tie = sequelize.define("group_item_tie", {}, {freezeTableName: true});
var item = sequelize.define("item", {
spn: { type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true },
}, { classMethods: {
associate: function (models) {
item.belongsToMany(models.group, { foreignKey: 'spn', through: models.group_item_tie });
}}
});
When I try to return some records with relationships, let’s say like this:
dbcontext.group.findAll({
where: { id: 6 },
include: [{
model: dbcontext.item,
as: 'items',
attributes: ['spn']
}]
})
I also get in result the fields from a tie table group_item_tie
:
[{
"id": 6,
"name": "abc",
"parentId": 5,
"createdAt": "2015-05-06T15:54:58.000Z",
"updatedAt": "2015-05-06T15:54:58.000Z",
"items": [
{ "spn": 1,
"group_item_tie": {
"createdAt": "2015-05-06 15:54:58.000 +00:00",
"updatedAt": "2015-05-06 15:54:58.000 +00:00",
"group_id": 6,
"spn": 1
}
},
{ "spn": 2,
"group_item_tie": {
"createdAt": "2015-05-06 15:54:58.000 +00:00",
"updatedAt": "2015-05-06 15:54:58.000 +00:00",
"group_id": 6,
"spn": 2
}
},
I see it in generated sql query. How to exclude those from select statement? I’ve tried a few other things but was not successful.
I hope there is something cleaner then just doing:
delete item.group_item_tie;
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:18 (3 by maintainers)
Top Results From Across the Web
Can't exclude association's fields from select statement in ...
I have the following code (simplified): var group = sequelize.define( ...
Read more >Can't exclude association's fields from select statement in ...
Coding example for the question Can't exclude association's fields from select statement in sequelize-sequelize.js.
Read more >Associations - Sequelize
The A.belongsToMany(B, { through: 'C' }) association means that a Many-To-Many relationship exists between A and B , using table C as junction ......
Read more >How To Use Sequelize with Node.js and MySQL - DigitalOcean
Then, you will create Sequelize associations for one-to-one, ... With this call, Sequelize will automatically perform an SQL query to the ...
Read more >Handling sensitive fields with sequelize.js - DEV Community
Alternatives to exclude certain fields from query results when using ... with a few things like TypeScript support and associations (so many ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
just use joinTableAttributes attribute