无法从 sequelize 中的 select 语句中排除关联字段
Can't exclude association's fields from select statement in sequelize
我有以下代码(简化):
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 });
}}
});
当我尝试return一些有关系的记录时,我们这样说:
dbcontext.group.findAll({
where: { id: 6 },
include: [{
model: dbcontext.item,
as: 'items',
attributes: ['spn']
}]
})
我也从领带中得到字段 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
}
},
我在生成的 sql 查询中看到它。如何从 select 语句中排除那些?我尝试了一些其他的东西,但没有成功。
我希望有比现在更干净的东西:
delete item.group_item_tie;
我会自己回答,因为它可能对将来的人有用。所以根据 #3664, #2974 and #2975 答案如下(感谢 mickhansen):
include: [{
model: dbcontext.item,
as: 'items',
attributes: ['spn'],
through: {
attributes: []
}
}]
很快就会记录下来。
through: {
attributes: []
}
适合我
我意识到这个帖子有点过时了,但由于它在 Google 搜索结果中排名很高,而且我自己也很难找到答案,所以我想我应该在这里添加它。
如果您使用 Model.getAssociatedModel()
或 Model.$get()
(对于 sequelize-typescript),当前列出的答案不适用于此用例。为了隐藏模型关联,您需要添加 joinTableAttributes: []
示例:
Model.getAssociatedModel({
joinTableAttributes: []
})
示例:
Model.$get('property', <any>{
joinTableAttributes: []
});
在 post 时,joinTableAttributes
未包含在 sequelize-typescript 类型中,因此 <any>
我有以下代码(简化):
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 });
}}
});
当我尝试return一些有关系的记录时,我们这样说:
dbcontext.group.findAll({
where: { id: 6 },
include: [{
model: dbcontext.item,
as: 'items',
attributes: ['spn']
}]
})
我也从领带中得到字段 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
}
},
我在生成的 sql 查询中看到它。如何从 select 语句中排除那些?我尝试了一些其他的东西,但没有成功。
我希望有比现在更干净的东西:
delete item.group_item_tie;
我会自己回答,因为它可能对将来的人有用。所以根据 #3664, #2974 and #2975 答案如下(感谢 mickhansen):
include: [{
model: dbcontext.item,
as: 'items',
attributes: ['spn'],
through: {
attributes: []
}
}]
很快就会记录下来。
through: {
attributes: []
}
适合我
我意识到这个帖子有点过时了,但由于它在 Google 搜索结果中排名很高,而且我自己也很难找到答案,所以我想我应该在这里添加它。
如果您使用 Model.getAssociatedModel()
或 Model.$get()
(对于 sequelize-typescript),当前列出的答案不适用于此用例。为了隐藏模型关联,您需要添加 joinTableAttributes: []
示例:
Model.getAssociatedModel({
joinTableAttributes: []
})
示例:
Model.$get('property', <any>{
joinTableAttributes: []
});
在 post 时,joinTableAttributes
未包含在 sequelize-typescript 类型中,因此 <any>