2016-08-13 136 views
1

我有四個型號:如何通過表格屬性去除?

Category 
Video 
VideoCategory 
VideoSchedule 

有了這些關係:

Category.belongsToMany(Video, { through: VideoCategory }) 
Video.belongsToMany(Category, { through: VideoCategory }) 
Video.hasOne(VideoSchedule) 
VideoSchedule.belongsTo(Video) 

我想要檢索類別目前預定影片的列表。我非常接近,但當我想要的是Category.idCategory.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; 

任何有識之士將不勝感激!

回答