Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

frogeyedman's avatar

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215

Hello people,

I'm currently working on a school project and ran in to this problem with my database, I want to create a relationship with videos and categories here are my two tables. The video table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->default('');
            $table->string('url')->default('');

            $table->integer('category_id')->unsigned()->nullable();
            $table->foreign('category_id')->references('id')->on('categories');

            $table->string('img_url')->default('');
            $table->integer('views')->nullable();
            $table->integer('rating')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videos');
    }
}

And the categories table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

When i run php artisan migrate:refresh in the console i get this error:


  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `videos` add constraint `videos_category_id_foreign` foreign key (`category_id`) references `categories` (`id`))



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




i dont see why because when i change this:

 $table->foreign('category_id')->references('id')->on('categories');

to this:

 $table->foreign('category_id')->references('id')->on('users');

It works and then i have no error's whatsoever, i hope someone can help me out with this!

0 likes
1 reply
frogeyedman's avatar
frogeyedman
OP
Best Answer
Level 1

I found out, i didn't knew this was posible!



<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
      // create categories table
  Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
  });

     //now  create videos table
    Schema::create('videos', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->default('');
        $table->string('url')->default('');

        $table->integer('category_id')->unsigned()->nullable();
        $table->foreign('category_id')->references('id')->on('categories');

        $table->string('img_url')->default('');
        $table->integer('views')->nullable();
        $table->integer('rating')->nullable();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('videos');
    Schema::dropIfExists('categories');
}
}

1 like

Please or to participate in this conversation.