本文介绍了Sequelize.js-“不与”相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据库获取完整数据时遇到一些问题。
这是我的模型:

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: '[email protected]',
  role: {
     id: 2,
     name: 'admin'
     description: 'ipsum ssaffa',
     permission: 30
  }
}

所以我正在使用:

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

但我得到的结果是err.message:角色未与用户关联

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我看到每个用户只有一个角色,所以您可以在角色模型中使用EmiratesTo或在用户模型中使用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;
};

这篇关于Sequelize.js-“不与”相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:23