Fahad
Fahad

Reputation: 65

(Laravel 8) errno: 150 "Foreign key constraint is incorrectly formed

I want to create a table that assists users to read the topics that order by categories; also, users select the categories that they want in order to see the interest topics associated with categories. For instance, when users go to BBC, they can see the topics that they want to see from categories.


categories Table

id name
1 sports
2 Tech
 public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id('id');
            $table->string('name');
        });

    }
?>

users

id name email
1 Fahad [email protected]
2 Jhon [email protected]
 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username')->unique();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('avatar')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }


category_user -> piviot table

id user_id category_id
1 1 2
2 2 1
 public function up()
    {
        Schema::create('category_user', function (Blueprint $table) {
            $table->foreignId('user_id')->constrained('cascade');
            $table->foreignId('category_id')->constrained('cascade');
        });
    }

Category -> Model

    public function User(){
        return $this->belongsToMany(User::class);
    }

Error

  SQLSTATE[HY000]: General error: 1005 Can't create table `athar_db`.`category_user` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `category_user` add constraint `category_user_user_id_foreign` foreign key (`user_id`) references `cascade` (`id`))

Upvotes: 1

Views: 1936

Answers (2)

steven7mwesigwa
steven7mwesigwa

Reputation: 6710

Please read the documentation first.
https://laravel.com/docs/8.x/migrations#foreign-key-constraints

The foreignId method is an alias for unsignedBigInteger while the constrained method will use conventions to determine the table and column name being referenced. If your table name does not match Laravel's conventions, you may specify the table name by passing it as an argument to the constrained method:

The issue is that you're providing the table name as 'cascade' instead of 'users'.
i.e

//Should be...
$table->foreignId('user_id')->constrained('users');
//Instead of...
$table->foreignId('user_id')->constrained('cascade');

Don't forget to correct the 'category_id' as well.

If you really wish to apply 'cascade' options, try:

$table->foreignId('user_id')
      ->constrained()
      ->onUpdate('cascade')
      ->onDelete('cascade');

Upvotes: 2

Shobi
Shobi

Reputation: 11461

use the cascadeOnDelete() Method to define the cascade

$table->foreignId('user_id')->constrained()->cascadeOnDelete() 

Upvotes: 0

Related Questions