Skip to content
Advertisement

Sequelize.js – “is not associated to”

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

User

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'
    });
};

Role

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
  }
}

So I’m using:

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

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

And the simple question – what’s wrong ? 🙂

*to handle models I’m using sequelize-cli

Advertisement

Answer

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

base on your json I see that each user only has one role, so you can either use belongsTo in role model or hasOne in user model

Should be something like this:

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;
};
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement