错误:找到循环依赖性。公司依赖于自身

时间:2018-10-20 19:28:01

标签: node.js sequelize.js

初始化模型时出现错误:

  const Companies = sequelize.define('companies', {
    id: {
        type: Sequelize.INTEGER(10).UNSIGNED,
        autoIncrement : true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING(255),
        allowNull: false
    },
    createdby_id_fk:{
        type: Sequelize.INTEGER(10).UNSIGNED,
        references: {
            model: "persons",
            key: "id"
        },
        allowNull: false
    },
});

Companies.associate = function (models) {

    this.belongsTo(models.persons, {
        constraints: false,
        foreignKey: 'createdby_id_fk',
        targetKey: 'id'
    });

};

这是Tasks模型

   const Tasks = sequelize.define('tasks', {
    id: {
        type: Sequelize.INTEGER(10).UNSIGNED,
        autoIncrement: true,
        primaryKey: true
    },event_name: {
        type: Sequelize.STRING(255),
        allowNull: false
    },
    task_function_id_fk: {
        type: Sequelize.INTEGER(10).UNSIGNED,
        allowNull: false,
        references: {
            model: "task_functions",
            key: "id"
        }
    },company_id_fk: {
        type: Sequelize.INTEGER(10).UNSIGNED,
        allowNull: false,
        references: {
            model: "companies",
            key: "id"
        }
    }

});

Tasks.associate = function (models) {

    this.belongsTo(models.companies, {
        constraints: false,
        foreignKey: 'company_id_fk',
        targetKey: 'id'
    });

};

这是开胃菜模型:

 const Appetizer= sequelize.define('appetizer', {
    id: {
        type: Sequelize.INTEGER(10).UNSIGNED,
        autoIncrement: true,
        primaryKey: true
    },
    company_id_fk:{
        type: Sequelize.INTEGER(10).UNSIGNED,
        references: {
            model: "companies",
            key: "id"
        },
        allowNull: false
    },
    type:{
        type: Sequelize.STRING(255),
        allowNull: false
    },


});

Appetizer.associate = function (models) {
    this.belongsTo(models.companies, {
        foreignKey: 'company_id_fk',
        constraints: false,
        targetKey: 'id'
    });


};

我得到的完整错误是:

  

错误初始化的模型-错误:找到循环依赖性。公司   取决于自己。依赖链:开胃菜->公司->   任务=>公司

at visit (/home/vijay/Work/test/ESS/node_modules/toposort-class/build/toposort.js:173:27)
at visit (/home/vijay/Work/test/ESS/node_modules/toposort-class/build/toposort.js:212:29)
at visit (/home/vijay/Work/test/ESS/node_modules/toposort-class/build/toposort.js:212:29)

我不确定为什么在这里看不到循环依赖性时会引发循环错误? 谢谢

0 个答案:

没有答案