gpt4 book ai didi

javascript - Sequelize 和嵌套结果?

转载 作者:行者123 更新时间:2023-11-30 11:59:04 24 4
gpt4 key购买 nike

Sequelize 允许数据关系,但我似乎看不到是否有办法让它返回由关系组成的查询结果?我可以将其作为两个查询来执行,但我很好奇 Sequelize 是否提供此功能?例如播放列表和播放列表条目:

    Playlist: sequelize.define('playlist', {
name: Sequelize.STRING,
description: Sequelize.STRING
}),

PlaylistEntry: sequelize.define('playlist_entry', {
playlist: Sequelize.INTEGER
//track: Sequelize.INTEGER
})

PlaylistEntry.belongsTo(
Playlist,
{ as: 'Playlist', foreignKey: { name: 'fk_playlist' }});

我希望得到的(伪代码):

查询:

Playlist.find(where: {id: playlistId}, nestedResult: true);

结果:

[{
id: 123,
name: 'abc'
playlistEntries: [{
id: 321,
track: 431
}]
}]

最佳答案

Playlist.find({
where: {},
include: [
{model: PlaylistEntry} //this should be PlaylistEntry model
]
})

http://docs.sequelizejs.com/en/latest/docs/models-usage/#eager-loading

关于javascript - Sequelize 和嵌套结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122909/

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