ho bisogno di usare chiave esterna per il mio database, ma non riesco a fare questo, dopo comando di migrazione corsa in linea di comando, ottengo questo errore:come utilizzare chiave esterna in laravel 5.1 migrazione
[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
Campioni migrazione: la migrazione
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();
});
fornitore:
Schema::create('suppliers', function (Blueprint $table) {
$table->Increments('id',true);
$table->string('supplier',50);
$table->timestamps();
});
cerco di che con la nuova migrazione per i campioni, ma unsucce ssful:
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');
});
cerco di correggere la lunghezza della chiave primaria a 10, ma senza successo ancora una volta
dico fare questo, ma infruttuoso –
Prima eseguire la migrazione dei fornitori, poi assaggiare migration.Cause senza fornitore migration migration_id non funzionerà come chiave esterna –