2016-08-13 6 views
1

私は4つのモデルがあります:私は現在スケジュール動画のカテゴリの一覧を取得したいテーブル属性からテーブルを削除するには?

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

:これらの関係で

Category 
Video 
VideoCategory 
VideoSchedule 

を。私は非常に近いですが、Sequelizeは、私が欲しいのは、Category.idCategory.nameです。

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'}であるここに私のSequelizeです。スルーテーブルから余分な属性を取り除くにはどうすればよいですか?私は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; 

任意の洞察力をいただければ幸いです!

答えて

関連する問題