2017-04-30 6 views
1

私は現在、学校のプロジェクトに取り組んでいて、データベースでこの問題に対処しました。 ビデオとカテゴリとの関係を作成したいと思います。 ビデオテーブル:Illuminate Database QueryException SQLSTATEHY000:一般的なエラー:1215

<?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'); 
} 
} 

とカテゴリテーブル:

<?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'); 
} 
} 

私はPHPの職人の移行を実行します。私はこのエラーを取得し、コンソールでリフレッシュ:

[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]:一般エラー:1215外部キー制約を追加できません

私は、この変更されたときに、なぜので、参照していけない:これまで

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

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

をそれが動作し、その後、私はエラーなしの一切を持って、私は誰かがこれで私を助けることを願って!

答えて

0

あなたはこれを使用して、カテゴリの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'); 
} 
} 
+0

はどうもありがとうございました、私はこれを知っていませんでした! – frogeyedman

+0

あなたは歓迎です –

関連する問題