Reputation: 75
I'm new to Laravel, and I'm having issues with Migrations.
The table name spellings are accurate but I still get the error.
The error states
SQLSTATE[HY000]: General error: 1005 Can't create table
first_db
.#sql-41c_2f
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tablefees
add constraintfees_academic_id_foreign
foreign key (academic_id
) referencesacademics
(academic_id
))
The error points to this file below:
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->integer('academic_id')->unsigned;
$table->integer('level_id')->unsigned;
$table->integer('fee_type_id')->unsigned;
$table->string('fee_heading',200)->nullable;
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feestype');
});
Is there something I'm doing wrong?
Upvotes: 1
Views: 98
Reputation: 2416
This foreign column should be unsigned.
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->unsignedInteger('academic_id');
$table->unsignedInteger('level_id');
$table->unsignedInteger('fee_type_id');
$table->string('fee_heading',200)->nullable();
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feestype');
});
Source: Laravel Database: Migrations Columns
Upvotes: 0
Reputation: 104
unsigned should be a function :
$table->integer('academic_id')->unsigned();
Upvotes: 1