0

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']);
}
1
  • Is your foreign key on question table the same type as the id column on domain? Commented Sep 4, 2018 at 14:02

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.