I have a problem with my Laravel migrations :(
when i'm running php artisan migrate, it stops on a foreign key.
first migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::enableForeignKeyConstraints();
Schema::create('fichefrais', function (Blueprint $table) {
$table->char('idVisiteur', 4);
$table->foreign('idVisiteur')->references('id')->on('visiteur');
$table->char('mois',6);
$table->primary(['idVisiteur', 'mois']);
$table->integer('nbJustificatifs');
$table->decimal('montantValide', 10, 2);
$table->date('dateModif');
$table->char('idEtat', 2);
$table->foreign('idEtat')->references('id')->on('etat');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('fichefrais');
}
and the second
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::enableForeignKeyConstraints();
Schema::create('lignefraishorsforfait', function (Blueprint $table) {
$table->integer('id');
$table->primary('id');
$table->char('idVisiteur', 4);
$table->char('mois',6);
$table->foreign('idVisiteur')->references('idVisiteur')->on('fichefrais');
$table->foreign('mois')->references('mois')->on('fichefrais');
$table->char('libelle', 100);
$table->date('date');
$table->decimal('montant', 10, 2);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('lignefraishorsforfait');
}
After running the command, I got this error :
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table gsb_larave.#sql-176_b9 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table lignefraishors forfait add constraint lignefraishorsforfait_mois_foreign foreign key (mois) references fichefrais (mois) on delete cascade on update cascade)[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table gsb_laravel.#sql-176_b9 (errno: 150 "Foreign key constraint is incorrectly formed")
fichefrais
and you are referencing it at two different FK onlignefraishorsforfait
. Maybe try$table->foreign(array('idVisiteur', 'mois')->references(array('idVisiteur', 'mois')->on('fichefrais');
– Andrew Nolan