Illuminate\Database\QueryException SQLSTATEHY000: General error: 1215

273
May 01, 2017, at 07:46 AM

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!

Answer 1

you should create your categories table before before videos Table

you can use this and delete your categories migrateion:

<?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');
}
}
READ ALSO
Angular.js geting data from mongodb

Angular.js geting data from mongodb

I have a problem with getting request in my api from Angularjs

350
execute server side script for file creations

execute server side script for file creations

Using nodejs, I am developing a small web application where want to create a file on server with a button click on browser

282
NodeJS regularly losing bindings

NodeJS regularly losing bindings

I'm using NodeJS + Typescript + Gulp + PouchDB for backend, and Typescript + Webpack + React + Sass for the client (note that the webpack is served by express and webpackDevMiddleware) and yarnRegularly I get this kind of error:

423