Migrations Laravel 5.4

Asked

Viewed 282 times

3

I’m having trouble trying to run my Migrations, when I run:

php Artisan migrate:refresh --Seed

Error occurs below:

[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');
}

1 answer

2

In Laravel 5.5 we will have a function called:

php artisan migrate:fresh

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.