We want to assign the RequestTypeID
in the Request
table to the RequestTypeID
column in the DefRequestType
table as a foreign key, but we get the following error.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `Request` add constraint `request_requesttypeid_foreign` foreign key (`RequestTypeID`) references `DefRequestType` (`RequestTypeID`))
We fixed the problem by fixing the table engine as MyISAM
, but we want to use the table engine as InnoDB
. Also, we are trying the migration process in Laravel 8 version. First I create the DefRequestType table, then I create the Request table. The reason I later changed the value of the DefRequestTypeID column to bigIncrements in the DefRequestType table was to assign two primaryKeys in the DefRequestType table. I also wanted to be able to give the DefRequestTypeID column an additional autoIncrement property.
Request table
Schema::create('Request', function (Blueprint $table) {
$table->bigIncrements('RequestID');
$table->unsignedBigInteger('RequestTypeID')->nullable(); <----------------------------
$table->unsignedInteger('CustomerID')->nullable();
$table->unsignedInteger('ApplicationID')->nullable();
$table->foreign('RequestTypeID')
->references('RequestTypeID')
->on('DefRequestType');
$table->foreign('ContentID')
->references('ContentID')
->on('Content');
});
DefRequestType table
Schema::create('DefRequestType', function (Blueprint $table) {
$table->unsignedInteger('DefRequestTypeID');
$table->unsignedBigInteger('RequestTypeID'); <----------------------------
$table->string('RequestDefinition');
$table->integer('Status')->default(1);
$table->integer('OldID');
$table->primary(['DefRequestTypeID','RequestTypeID']);
});
Schema::table('DefRequestType', function (Blueprint $table) {
$table->bigIncrements('DefRequestTypeID')->change();
});
We would appreciate it if you could help.