laravel-eloquent – Migrations Laravel 5.4

Question:

I have a problem 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');
}

Answer:

In Laravel 5.5 we will have a function called:

php artisan migrate:fresh

Which will drop the tables and start a new migration.

This error happens because the refresh command tries to give a rollback and many times, due to some previous error, it cannot actually run the scripts..

As in version 5.4 we still don't have migrate:fresh try to delete all tables manually or run drop and create in the database before running migrate again.

Scroll to Top