gpt4 book ai didi

sequelize.js - 如何通过表属性删除?

转载 作者:行者123 更新时间:2023-12-03 22:18:12 26 4
gpt4 key购买 nike

我有四个模型:

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'} 。如何从直通表中删除额外的属性?我尝试在我的 through: { attributes: [] } 语句中使用 include,但无济于事。为了彻底,这里是 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;

任何见解将不胜感激!

最佳答案

对于遇到此问题的任何其他人,这是 Sequelize 中的一个错误。看起来它会在 4.x 中修复,但不会在 3.x 中修复。

https://github.com/sequelize/sequelize/issues/5590

关于sequelize.js - 如何通过表属性删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com