1 回答
TA贡献1784条经验 获得超8个赞
DBTweet.findAll({
where: { userID: followingUserID },
include: [{
model: DBUser,
as: 'Users', //here goes the alias as well
include: [{
model: DBFollower,
as: 'Followers' //here is goes the alias of the association
}],
}]
});
module.exports = (sequelize, DataTypes) => {
const DBUser = sequelize.define('DBUser', {
// your attributes
});
DBUser.associate = (models) => {
DBUser.hasMany(models.DBFollower, { as: 'Followers', foreignKey: 'your_key' });
// DBUser.belongsTo(models.DBFollower, { as: 'Followers', foreignKey: 'branch_id' });
};
return DBUser;
};
更新:
现在与您的协会:
DBUser.hasMany(DBTweet, { as: 'Users', foreignKey: 'userID', onDelete: 'cascade' })
DBTweet.belongsTo(DBUser, { as: 'Users', foreignKey: 'userID', onDelete: 'cascade' })
DBUser.hasMany(DBFollower, { as: 'followingUserIDAlias', foreignKey: 'followingUserID', onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followingUserIDAlias', foreignKey: 'followingUserID', onDelete: 'cascade' })
DBUser.hasMany(DBFollower, { as: 'followerUserIDAlias', foreignKey: 'followerUserID', onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followerUserIDAlias', foreignKey: 'followerUserID', onDelete: 'cascade' })
添加回答
举报