Home:ALL Converter>Sequelize Cyclic dependency found

Sequelize Cyclic dependency found

Ask Time:2020-03-26T00:27:49         Author:Johnny Bra

Json Formatter

I get this weird error when I was trying to sync my database:

Unhandled rejection Error: Cyclic dependency found. roles is dependent of itself.
Dependency chain: roles -> users => roles

I have this following junction table model called Permission

const Permission = db.define('permission', {
    id: {
        type: type.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    role_id: {
        type: type.INTEGER,
        references: {
            model: 'roles',
            key: 'id',
        }
    },
    resource_id: {
        type: type.INTEGER,
        references: {
            model: 'resources',
            key: 'id',
        }
    },
});

Why does this error happen? And how can I fix it? In my User model, User has one Role :

const User = db.define('user', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    role_id : {
        type: Sequelize.INTEGER,
        references: {
            model: 'roles',
            key: 'id',
        }
    }
});

User.hasOne(Role)

Edit : Here's my Role model:

const Role = db.define('role', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: Sequelize.STRING,
})

module.exports = Role

Author:Johnny Bra,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60853047/sequelize-cyclic-dependency-found
yy