我正在扩展现有的Sequelize代码,使用INNER JOIN
和users
表之间的user_group_access
,其中包含我需要使用的关系ACL类型数据CASL:
const User = sequelize.define('users', {
user_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: Sequelize.STRING
},
}, {
timestamps: false
}, {
classMethods: {
associate: function(models) {
User.hasOne(models.UserGroupAccess, { foreignKey: 'user_id' } );
}
},
instanceMethods: {
}
}, {
underscored: true
});
...和:
const UserGroupAccess = sequelize.define('user_group_access', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
user_group_id: {
type: Sequelize.INTEGER
},
user_id: {
type: Sequelize.INTEGER,
references: {
model: User,
key: 'user_id'
}
}
}, {
timestamps: false
}, {
classMethods: {
associate: function(models) {
UserGroupAccess.belongsTo(models.User, { foreignKey: 'user_id' } );
}
},
instanceMethods: {
}
}, {
underscored: true
});
值得一提的是,我正在尝试 来了解Sequelize,因此上面包含了实验代码。
然后:
UserGroupAccess.findByEmail = function(email) {
return this.findAll({
where: {
email: email
},
include: [{
model: User,
required: true
}]
})
}
当我尝试登录时,我得到:
SequelizeEagerLoadingError:用户未与 user_group_access!
如果我交换表名,则会得到相同的错误,但相反。
鉴于代码的冗长和难以理解,我正在努力为使用Sequelize辩护!
想法?