gpt4 book ai didi

sequelize.js - sequelize 关联键是大写的响应

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

我该如何制作 Music是`音乐?

{
id: 4
name: "playlist 1"
created_at: "2015-04-21T21:43:07.000Z"
updated_at: "2015-04-23T20:44:50.000Z"
Music: [
{
id: 12
name: "Deorro - Five Hours (Static Video) [LE7ELS]"
video_id: "K_yBUfMGvzc"
thumbnail: "https://i.ytimg.com/vi/K_yBUfMGvzc/default.jpg"
created_at: "2015-04-22T21:46:21.000Z"
updated_at: "2015-04-22T21:46:21.000Z"
playlist_id: 4
}
]
}

我的查询类似于:
.get(function (req, res) {
db.Playlist.findAll({
include: [db.Music]
}).then(function (playlists) {
if(!playlists) {
res.status(404).json({message: 'No playlist found!'});
return;
}

res.json(playlists)
})
})

播放列表模型:
module.exports = function(sequelize, DataType) {
var Playlist = sequelize.define('Playlist', {
name: DataType.STRING
}, {
classMethods: {
associate: function(models) {
Playlist.hasMany(models.Music, { foreignKey: 'playlist_id' });
}
},
tableName: 'playlists',
underscored: true
});

return Playlist;
};

音乐模型:
module.exports = function(sequelize, DataType) {
var Music = sequelize.define('Music', {
name: DataType.STRING,
video_id: DataType.STRING,
thumbnail: DataType.STRING
}, {
classMethods: {
associate: function(models) {
Music.belongsTo(models.Playlist);
}
},
tableName: 'musics',
underscored: true
});

return Music;
}

最佳答案

更改型号名称中的大小写:

sequelize.define('music', ...)

或提供 as关联中的关键:
Playlist.hasMany(Music, { as: 'music' })

关于sequelize.js - sequelize 关联键是大写的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29835154/

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