I have a 'comment' model as following
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();
a 'user' model as following
var model = sequelize.define('user', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true
},
openid: {
type: DataTypes.STRING,
allowNull: false
}
});
model.sync();
Then association as following
comment.belongsTo( user, { foreignKey:"author_openid", targetKey:"openid" } )
user.hasMany( comment, { foreignKey:"author_openid", sourceKey:"openid" } )
I want make an association between 'user' and 'comment' as above and do a query as following
user.findAll({
include: [{
model: comment
}],
raw: true
})
What's wrong with my code?
答案 0 :(得分:0)
A quick glance at the docs, make it seem like your include should just be an array with the model not an object.
const users = await user.findAll({
include: [ comment ],
raw: true
})