TheDevGuy Marc
TheDevGuy Marc

Reputation: 89

Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Hey im currently working on a Laravel 6 Database migration but when I execute a php artisan migrate:fresh the following error jumps at me: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table category_post add constraint category_post_category_id_foreign foreign key (category_id) references id (categories) on delete cascade on update cascade)

The following did I check:

Here is my Migration Code I hope you can see the mistake I made because i can't find it.

        // Table for storing Blog Posts
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('slug');
            $table->string('read_time');
            $table->string('summary');
            $table->string('body');
            /*$table->timestamps('created_at');
            $table->timestamps('updated_at');*/
            $table->timestamps();
        });

        // Table for storing categories
        Schema::create('categories', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->string('name')->unique();
          $table->string('slug')->unique();
          $table->string('description');
          $table->timestamps();
        });

        // Table for association of Categories with Blog Posts
        Schema::create('category_post', function (Blueprint $table) {
          $table->bigInteger('category_id');
          $table->bigInteger('post_id');

          $table->foreign('category_id')->references('id')->on('categories')
            ->onUpdate('cascade')->onDelete('cascade');
          $table->foreign('post_id')->references('id')->on('posts')
            ->onUpdate('cascade')->onDelete('cascade');

          $table->primary(['post_id', 'category_id']);
        });

        // Table for association of Users with Blog Posts
        Schema::create('user_post', function (Blueprint $table) {
          $table->bigInteger('user_id');
          $table->bigInteger('post_id');

          $table->foreign('user_id')->references('id')->on('users')
            ->onUpdate('cascade')->onDelete('cascade');
          $table->foreign('post_id')->references('id')->on('posts')
            ->onUpdate('cascade')->onDelete('cascade');

          $table->primary(['user_id', 'post_id']);
        });

The goal after all is to associate categories and users to posts so that I can have tags and users on posts.

Thank you in advance for your help and kindness, I'm pretty new to Laravel but I have a good understanding of PHP, so it would be really nice if you could explain what I did wrong :)

Upvotes: 2

Views: 1168

Answers (1)

Script47
Script47

Reputation: 14550

According to the documentation, bigIncrements represents:

Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.

Therefore, your foreign keys need to match that (unsignedBigInteger()).

Upvotes: 4

Related Questions