mysql - SequelizeEagerLoadingError : Role is not associated to User

标签 mysql sequelize.js associations

我将 Sequelize 与 MYSQL 和 express 结合使用。 当我尝试使用 include 从相关模型中选择数据时,出现 SequelizeEagerLoadingError: Role is not associated to User

我有两个模型,用户和角色。用户通过 RoleUser 属于 ToMany Roles,角色通过 RoleUser 属于 ToMany 用户。这是我的模型:

//用户模型

'use strict';

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: {type:DataTypes.UUID,allowNull:false,unique:true,primaryKey:true},
    UserName: {type:DataTypes.STRING,unique:true,allowNull:false},
     Email: {type:DataTypes.STRING,allowNull:false,unique:true,validate: { isEmail: {msg: "Invalid Email"} }},
    Password: {type:DataTypes.STRING,allowNull:false},

  }, {});
  User.associate = function(models) {


        User.belongsToMany(models.Role,
            {
        through: 'RoleUser',
        foreignkey:'UserId'

    })




 };
  return User;
};

//Role Model
'use strict';
module.exports = (sequelize, DataTypes) => {
  const Role = sequelize.define('Role', {
    id: {type:DataTypes.UUID,allowNull:false,unique:true,primaryKey:true},
    Name: DataTypes.STRING,
    Description: DataTypes.STRING
  }, {});
  Role.associate = function(models) {
    Role.belongsToMany(models.User,
        {
            through:'RoleUser',
            foreignKey:'RoleId'

        }),


  };
  return Role;
};

//My query on user model

xports.index = function(req, res) {
  return User.findAll({ 
          include: [{
          model: Role,

        }],

    attributes:{
        exclude: ['Password','createdAt','updatedAt']

    }}).then((users)=>{
    console.log(users)
    }).catch(error=>{
    console.log(error);
    })

//错误

 SequelizeEagerLoadingError: Role is not associated to User!

 **STATUS CODE: 200(OK)**

 I am getting same error in other models in the entire project. According to me the associations is correctly established and I can see in the PHPMyAdmin Relation View.

 *Express Version: 4.16.4
 Sequelize Version: 4.31.2
 Mysql2 version: 1.6.1*

 Any help. What am I doing wrong.

最佳答案

好的,所以您需要指定如何调用包含。

User.associate = function(models) {
  User.belongsToMany(models.Role,{
    through: 'RoleUser',
    foreignkey:'UserId',
    as: 'Roles'
  })
};

如果需要,您需要以其他方式从角色到用户,以及任何其他关联。然后根据您的查询:

User.findAll({ 
  include: [{
    model: Role,
    as: 'Roles'
  }],
  attributes:{ exclude: ['Password','createdAt','updatedAt'] }
})

关于mysql - SequelizeEagerLoadingError : Role is not associated to User,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55049155/

相关文章:

ruby-on-rails - 在 Rails 中访问未保存的 parent 和 child 的祖 parent

带有 Sequelize : ER_BAD_DB_ERROR: Unknown database 的 MySQL

java - Db中正确写入,但Sql时出现 "Illegal operation on empty result set."错误

mysql - 学习 MySQL、Python - 跳过重复

MySQL 错误 1064 语法但一切似乎都很好

javascript - Sequelizejs : no auto-generation of methods from associations

javascript - 序列化类型错误: defineCall is not a function

node.js - 使用sequelize关联不同的模型?

class - 是否建议在 UML 类图中与枚举类建立关联?

mysql - COUNT(*) 和 LEFT JOIN