1
我有四个型号:如何通过表格属性去除?
Category
Video
VideoCategory
VideoSchedule
有了这些关系:
Category.belongsToMany(Video, { through: VideoCategory })
Video.belongsToMany(Category, { through: VideoCategory })
Video.hasOne(VideoSchedule)
VideoSchedule.belongsTo(Video)
我想要检索类别目前预定影片的列表。我非常接近,但当我想要的是Category.id
和Category.name
时,Sequelize不断给我直通表的属性。这里是我的Sequelize:
Category.findAll({
attributes: ['id', 'name'],
raw: true,
group: 'Category.id',
order: 'Category.name',
include: [
{
model: Video,
attributes: [],
where: { active: true },
through: { attributes: [] },
include: [
{
model:
VideoSchedule,
attributes: [],
where: { site_id: 106 }
}
]
}
]
}).then(function(cats) { console.log(cats); });
这里是我得到的输出样本:
{ id: 1,
name: 'Comedy',
'Videos.VideoCategory.category_id': 1,
'Videos.VideoCategory.video_id': 962 },
{ id: 2,
name: 'Drama',
'Videos.VideoCategory.category_id': 2,
'Videos.VideoCategory.video_id': 914 }
我真正想要的只是{ id: 1, name: 'Comedy' }, { id: 2, name: 'Drama'}
。我如何摆脱直通表的额外属性?我在include
声明中尝试使用through: { attributes: [] }
,但无济于事。只是要彻底,这里是Sequelize生成的SQL语句:
SELECT
`Category`.`id`,
`Category`.`name`,
`Videos.VideoCategory`.`category_id` AS `Videos.VideoCategory.category_id`,
`Videos.VideoCategory`.`video_id` AS `Videos.VideoCategory.video_id`
FROM
`categories` AS `Category`
INNER JOIN (`video_categories` AS `Videos.VideoCategory`
INNER JOIN `videos` AS `Videos` ON `Videos`.`id` = `Videos.VideoCategory`.`video_id`)
ON `Category`.`id` = `Videos.VideoCategory`.`category_id`
AND `Videos`.`active` = true
INNER JOIN `video_schedules` AS `Videos.VideoSchedule`
ON `Videos`.`id` = `Videos.VideoSchedule`.`video_id`
AND `Videos.VideoSchedule`.`site_id` = 106
GROUP BY Category.id
ORDER BY Category.name;
任何有识之士将不胜感激!