首页 > 解决方案 > 无法添加或更新子行:外键约束失败 ON DELETE CASCADE ON UPDATE CASCADE

问题描述

当我在带有 mid、cid、ordertotal 的订单表中插入数据时遇到问题,然后它显示类似这样的错误无法添加或更新子行:外键约束失败 ( coindb. orders, CONSTRAINT orders_ibfk_1FOREIGN KEY ( MID) REFERENCES machine( ID) ON DELETE CASCADE ON UPDATE CASCADE)"。谁能帮我纠正这个问题

   here is orders table  
  'use strict';
   module.exports = (sequelize, DataTypes) => {
   var Model = sequelize.define('orders', {
   id: { type: DataTypes.INTEGER, field: 'id', primaryKey: true}
   mid: { type: DataTypes.INTEGER, field: 'mid'},
   cid: { type: DataTypes.INTEGER, field: 'cid'},
   wid: { type: DataTypes.INTEGER, field: 'wid'},
   oid: { type: DataTypes.INTEGER, field: 'oid'},
   status: { type: DataTypes.CHAR, field: 'status'},
   options: { type: DataTypes.STRING, field: 'options'},
   starttime: { type: DataTypes.DATE,field: 'starttime'},
   duration: { type: DataTypes.INTEGER, field: 'duration'},
   ordertotal: { type: DataTypes.INTEGER, field: 'ordertotal'},
   counter: { type: DataTypes.INTEGER, field: 'counter'},
   closetime: { type: DataTypes.DATE, field: 'closetime'}
    }, {
   tableName: 'orders',
   timestamps: false,
    });  
    Model.prototype.toWeb = function (pw) {
    let json = this.toJSON();
    return json;
    };
    return Model;
    };

    and machine table 
     'use strict';
   module.exports = (sequelize, DataTypes) => {
   var Model = sequelize.define('machine', {
   id:{type:DataTypes.INTEGER,field:'id',primaryKey:true,utoIncrement:true}
   type : {type : DataTypes.STRING, field : 'type'},
   bid : {type : DataTypes.INTEGER, field : 'bid'},
   },
   {
   tableName: 'machine',
   timestamps: false
   });
   Model.associate = function(models){     
   this.branches = this.belongsTo(models.branch, {foreignKey: 'bid'});
   };
   Model.prototype.toWeb = function (pw) {
     let json = this.toJSON();
     return json;
    };
    return Model;
     };

标签: mysqlnode.js

解决方案


推荐阅读