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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办