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

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');
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题