Reputation: 429
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 tabletblquestion
add constrainttblquestion_que_csd_id_foreign
foreign key (que_csd_id
) referencestblcsdomain
(csd_id
))
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);
});
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']);
}
Upvotes: 0
Views: 794
Reputation: 217
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();
Upvotes: 2
Reputation: 33186
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.
Upvotes: 0