Sequelize.js - “不关联到"

2022-01-19 00:00:00 node.js database javascript sequelize.js

我在从 db 获取完整数据时遇到了一些问题.那是我的模型:

I have some issue with getting full data from db. That are my models:

用户

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('user', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'ID'
        },
        password: {
            type: DataTypes.STRING(255),
            allowNull: false,
            field: 'password'
        },
        email: {
            type: DataTypes.STRING(255),
            allowNull: false,
            unique: true,
            field: 'email'
        },
        roleId: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            references: {
                model: 'role',
                key: 'ID'
            },
            field: 'role_id'
        }
    }, {
        timestamps: false,
        tableName: 'user'
    });
};

角色

module.exports = function(sequelize, DataTypes) {
return sequelize.define('role', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        field: 'ID'
    },
    name: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: true,
        field: 'name'
    },
    description: {
        type: DataTypes.STRING(255),
        allowNull: false,
        field: 'description'
    },
    permission: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        field: 'permission'
    }
}, {
    timestamps: false,
    tableName: 'role',
});};

我想获取一个特定用户的对象,包括所有角色内容.有点像

I want to get object of one specific user including all role content. Somethink like

{
  id: 4,
  password: 'xxx',
  email: 'adsads@saas.com',
  role: {
     id: 2,
     name: 'admin'
     description: 'ipsum ssaffa',
     permission: 30
  }
}

所以我正在使用:

User.findOne( { where: { id: req.userId }, include: [ Role ] } ).then( user =>{...});

但我得到了结果 err.message: "role is not associated to user"

but I get in the result err.message: "role is not associated to user"

还有一个简单的问题 - 怎么了?:)

And the simple question - what's wrong ? :)

*处理我正在使用 sequelize-cli 的模型

*to handle models I'm using sequelize-cli

推荐答案

你得到这个错误是因为你没有在模型之间添加关联

You get this error because you didn't add associate between the models

根据你的json我看到每个用户只有一个角色,所以你可以在角色模型中使用belongsTo或在用户模型中使用hasOne

应该是这样的:

User.js

module.exports = function(sequelize, DataTypes) {
var user =  sequelize.define('user', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        field: 'ID'
    },
    password: {
        type: DataTypes.STRING(255),
        allowNull: false,
        field: 'password'
    },
    email: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: true,
        field: 'email'
    },
    roleId: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        references: {
            model: 'role',
            key: 'ID'
        },
        field: 'role_id'
    }
}, {
    timestamps: false,
    tableName: 'user'
});
    user.associate = function(models) {
        user.hasOne(models.role, {foreignKey: 'id',sourceKey: 'roleId'});

    }
    return user;
};

Role.js

module.exports = function(sequelize, DataTypes) {
    var role = sequelize.define('role', {
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        field: 'ID'
    },
    name: {
        type: DataTypes.STRING(255),
        allowNull: false,
        unique: true,
        field: 'name'
    },
    description: {
        type: DataTypes.STRING(255),
        allowNull: false,
        field: 'description'
    },
    permission: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        field: 'permission'
    }
    }, {
        timestamps: false,
        tableName: 'role',
    });
    role.associate = function(models) {
        user.belongsTo(models.role, {foreignKey: 'id'});

    }
    return role;
};

相关文章