我正在尝试与Sequelize建立许多关联。我有两个模型,User和Artist。用户可以关注许多艺术家,并且艺术家可以成为以下列表中许多用户的一部分:
用户:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
userUID: DataTypes.STRING,
username: DataTypes.STRING,
mod: DataTypes.INTEGER
}, {});
User.associate = function(models) {
// associations can be defined here
User.belongsToMany(models.Artist, { through: models.UserArtist, foreignKey: 'userID', otherKey: 'artistID' });
};
return User;
};
艺术家:
module.exports = (sequelize, DataTypes) => {
const Artist = sequelize.define('Artist', {
followers: DataTypes.INTEGER,
imageURL: DataTypes.STRING,
name: DataTypes.STRING,
artistUID: DataTypes.STRING
}, {});
Artist.associate = function(models) {
// associations can be defined here
Artist.belongsToMany(models.User, { through: models.UserArtist, foreignKey: 'artistID', otherKey: 'userID' });
};
return Artist;
};
我有一个联接表:
module.exports = (sequelize, DataTypes) => {
const UserArtist = sequelize.define('UserArtist', {
userID: DataTypes.INTEGER,
artistID: DataTypes.INTEGER
}, {});
UserArtist.associate = function(models) {
// associations can be defined here
};
return UserArtist;
};
但是当我尝试和急切加载UserArtist时,出现错误
Artist.findAll({
where: { artistUID: req.params.id },
include: [
{ model: UserArtist }
]
SequelizeEagerLoadingError:UserArtist与Artist没有关联!
我在做什么错?谢谢!