A4family
A4family

Reputation: 73

Cannot delete or update a parent row: a foreign key constraint fails even there is onDelete Cascade and onUpdate cascade?

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

users Table
Schema::create('users', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('name');
  $table->string('email')->unique();
  $table->timestamp('email_verified_at')->nullable();
  $table->string('password');
  $table->rememberToken();
  $table->timestamps();
});
cv table

 Schema::create('cvs', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('user_id')->nullable();
  $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
  $table->string('name')->nullable();
  $table->string('contact')->nullable();
  $table->string('contact2')->nullable();
  $table->string('gender')->nullable();
  $table->string('email')->unique()->nullable();
  $table->string('paddress')->nullable();
});

Upvotes: 0

Views: 160

Answers (2)

Benyou1324
Benyou1324

Reputation: 521

In this situation you need to migrate the users table first, then the cvs to do that:

php artisan migrate --path=/database/migrations/selected

if you have run migration before you need to migrate

php artisan migrate --path=/database/migrations/2014_10_12_000000_create_users_table.php

then run

php artisan migrate

It'll automatically migrate all the rest of the tables for you

Hope this helps

Upvotes: 0

Adam Kozlowski
Adam Kozlowski

Reputation: 5906

Reason can be:

  • your migration of CSV is before Users. Take a look migration names (and dates). It should be:

    2019_02_01_101010_create_users_table.php

    2019_02_02_101010_create_csv_table.php (look, date is 2019_02_02)

Then fire php artisan:migrate

Be sure that in other migrations you have proper dates. Migration can not work if one table want relation with table that was not created yet.

Good luck!

Upvotes: 1

Related Questions