dongxiong1941
2017-04-30 19:17
浏览 497
已采纳

Illuminate \ Database \ QueryException SQLSTATEHY000:常规错误:1215

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!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doutangu4978 2017-04-30 19:26
    最佳回答

    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');
    }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题