1
votes

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?

2
It looks like the constraint for academic_id throws this. It could be that the academics table doesn't exist, doesn't have that field, has that field but with a different definition (char, signed int, something like that) or something like that?Nanne
Foreign key column and reference column must be same data type, and in your migration $table->integer('academic_id')->unsigned() use unsigned like thisGihan Lakshan
refer this: stackoverflow.com/questions/26437342/…user9628338

2 Answers

1
votes

unsigned should be a function :

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

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