mercredi 23 mai 2018

laravel migration doesn't follow timestamp order

I am working with Laravel 5.4 trying to perform a migration into Mysql. I have read that Migrate follows the execution based on the timestamp of the migration files. But this is not the case, I have the following error:

 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 
(SQL: alter table `aplicantes` add constraint `aplicantes_pais_id 
_foreign` foreign key (`pais_id`) references `pais` (`id`))

This is my migrations files:

2014_01_01_100004_create_pais_table.php
2014_01_01_100005_create_departamentos_table.php
2014_01_01_100006_create_aplicantes_table.php

And this is where migration complains:

class CreateAplicantesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('aplicantes', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nombre');
        $table->unsignedInteger('pais_id');
        $table->foreign('pais_id')->references('id')->on('pais');
        $table->timestamps();
    });
}

As I can see from here, Migration is not following the order convention, and when I go to my DB, I only see the Aplicantes table created, which is the third in the order. I guess that's why migration fails because Aplicantes table is being created first, which references a table that does not exist at the moment it is created.

How can I overcome this problem? As I can see, Migrations is following an alphabetical order, should I consider this a bug?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire