Solomon Mbak
Solomon Mbak

Reputation: 75

On Laravel I get the error errno: 150 "Foreign key constraint is incorrectly formed"

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 table fees add constraint fees_academic_id_foreign foreign key (academic_id) references academics (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

Answers (2)

Sand Of Vega
Sand Of Vega

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

Jodeveloper8
Jodeveloper8

Reputation: 104

unsigned should be a function :

$table->integer('academic_id')->unsigned();

Upvotes: 1

Related Questions