Migrations Laravel 5.4

3

I'm having trouble trying to run my migrations when I run:

  

php artisan migrate: refresh --seed

The error below occurs:

[Illuminate\Database\QueryException]
  SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists 'cities')



  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails

CreateCitiesTable

public function up()
    {
        Schema::create('cities', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cities');
    }

CreatePlacesTable

public function up()
{
    Schema::create('places', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('city_id')->unsigned();
        $table->foreign('city_id')->references('id')->on('cities');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories');
        $table->integer('company_id')->unsigned();
        $table->foreign('company_id')->references('id')->on('companies');
        $table->string('name');
        $table->text('description');
        $table->string('icon');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('places');
}

CreateClientsTable

public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('city_id')->unsigned();
        $table->foreign('city_id')->references('id')->on('cities');
        $table->string('phone');
        $table->text('address');
        $table->string('zipcode');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('clients');
}

CreateCompaniesTable

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('city_id')->unsigned();
        $table->foreign('city_id')->references('id')->on('cities');
        $table->string('phone');
        $table->text('address');
        $table->string('zipcode');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('companies');
}
    
asked by anonymous 19.06.2017 / 02:57

1 answer

2

In Laravel 5.5 we will have a function named:

php artisan migrate:fresh

That will make a drop in the tables and start a new migration.

This error happens because the command refresh tries to give a rollback and often due to some previous error, can not actually run the scripts ..

As in version 5.4 we do not yet have migrate:fresh try to delete all tables manually or run drop and create in the database before running migrate again.

    
19.06.2017 / 15:41