0
votes

So I get this error:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table yamldb.#sql-3928_6ea (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table tblquestion add constraint tblquestion_que_csd_id_foreign foreign key (que_csd_id) references tblcsdomain (csd_id))

Table 1

Schema::create('tblquestion', function (Blueprint $table) {
    $table->increments('que_id');
    $table->string('que_name', 128);
    $table->string('que_identifier', 128);
    $table->string('que_version', 50);
    $table->char('que_content');
    $table->char('que_answers');
    $table->integer('que_grd_id')->unsigned();
    $table->integer('que_quf_id')->unsigned();
    $table->integer('que_lan_id')->unsigned();
    $table->boolean('que_mandatory', false);
    $table->char('que_thisisinformatics');
    $table->char('que_translations');
    $table->char('que_explanation');
    $table->char('que_background_info');
    $table->integer('que_cou_id')->unsigned();
    $table->boolean('que_allow_share', false);
    $table->integer('que_source_cou_id')->unsigned();
    $table->integer('que_source_que_id');
    $table->mediumInteger('que_csd_id')->unsigned();
    $table->string('que_token', 32);    
});

Table 2

Schema::create('tblcsdomain', function (Blueprint $table) {
    $table->increments('csd_id');
    $table->string('csd_name', 128);
    $table->string('csd_token', 128);
 });

Migration

 Schema::table('tblquestion', function (Blueprint $table) {
    $table->foreign('que_csd_id')->references('csd_id')->on('tblcsdomain');
}

Also I am trying to add FK to already existing columns. And Laravel adds the FK but on rollback it doesn't remove them.

Schema::table('tblquestion', function (Blueprint $table) {
    $table->dropForeign(['que_csd_id']);
}
2
Is your foreign key on question table the same type as the id column on domain?James Cook

2 Answers

2
votes

Your foreign keys need to be the same type as your primary keys.

You either need to use

$table->mediumIncrements('csd_id');

In your Table 2 migration for the id column. Or change the type of

$table->mediumInteger('que_csd_id')->unsigned();

To

$table->integer('que_csd_id')->unsigned();
0
votes

The types of both columns are not the same.

tblquestion.que_csd_id is a medium integer.
tblcsdomain.csd_id is a normal integer.

You will have to change either one to the type of the other for this to work.