[英]Sequelize can't make 'belongsto' and 'hasmany' association
我有一個'評論'模型如下
var comment = sequelize.define('comment', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
author_openid:{
type: DataTypes.STRING,
allowNull: false
},
text: { type:DataTypes.TEXT, allowNull:false }
});
model.sync();
“用戶”模型如下
var model = sequelize.define('user', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
openid: {
type: DataTypes.STRING,
allowNull: false
}
});
model.sync();
然后關聯如下
comment.belongsTo( user, { foreignKey:"author_openid", targetKey:"openid" } )
user.hasMany( comment, { foreignKey:"author_openid", sourceKey:"openid" } )
我希望如上所述在'user'和'comment'之間建立關聯,並執行如下查詢
user.findAll({
include: [{
model: comment
}],
raw: true
})
我的代碼出了什么問題?
快速瀏覽一下docs ,使您的include看起來應該只是模型而不是對象的數組。
const users = await user.findAll({
include: [ comment ],
raw: true
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.