I need to use foreign key for my database but I can't do this, after run migration command in command line, I get this error :
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
samples
add constraint s amples_supplier_id_foreign foreign key (supplier_id
) referencessuppliers
(id
))[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Samples migration :
Schema::create('samples', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('variety',50);
$table->integer('supplier_id')->unsigned();
$table->foreign('supplier_id')->references('id')->on('suppliers');
$table->string('lot_number');
$table->date('date');
$table->integer('amount');
$table->integer('unit_id')->unsigned();
$table->foreign('unit_id')->references('id')->on('unit');
$table->string('technical_fact');
$table->string('comments');
$table->string('file_address');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('category');
$table->timestamps();
});
Supplier migration :
Schema::create('suppliers', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('supplier',50);
$table->timestamps();
});
I try to that with new migration for samples, but unsuccessful :
Schema::create('samples', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('variety',50);
$table->integer('supplier_id')->unsigned();
$table->string('lot_number');
$table->date('date');
$table->integer('amount');
$table->integer('unit_id')->unsigned();
$table->string('technical_fact');
$table->string('comments');
$table->string('file_address');
$table->integer('category_id')->unsigned();
$table->timestamps();
});
Schema::table('samples', function($table) {
$table->foreign('supplier_id')->references('id')->on('suppliers');
$table->foreign('unit_id')->references('id')->on('unit');
$table->foreign('category_id')->references('id')->on('category');
});
I try to fix length of the primary key to 10, but unsuccessful again