2017-01-16 18 views
0

ブログアプリケーションにコメントシステムを追加しようとしていますが、現在のcommentsマイグレーションファイルとは関係のないコメント移行を実行しようとするとこのエラーが発生しますが、以前post_tag移行ファイルphp artisan migrateコマンドを実行するとエラーが発生する

[Illuminate\Database\QueryException] 
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'post_tag' already exists (SQL: 
    create table `post_tag` (
     `id` int unsigned not null auto_increment primary key, 
     `post_id` int unsigned not null, 
     `tag_id` int unsigned not null 
    ) default character set utf8 collate utf8_unicode_ci) 

は、これは私のcomments移行ファイル

<?php 

//2017_01_16_101128_create_comments_table.php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateCommentsTable extends Migration 
{ 
/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('comments', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('email'); 
     $table->text('comment'); 
     $table->boolean('approved'); 
     $table->integer('post_id')->unsigned(); 
     $table->timestamps(); 

    }); 

    Schema::table('comments', function($table){ 
      $table->foreign('post_id')->references('id')->on('posts')-> 
        onDelete('cascade'); 
     }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::dropForeign(['post_id']); 
    Schema::drop('comments'); 
} 
} 

であり、これは私のpost_tag移行ファイル

01です
<?php 

2016_12_18_230831_create_post_tag_table.php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreatePostTagTable extends Migration 
{ 
/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('post_tag', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('post_id')->unsigned(); 
     $table->foreign('post_id')->references('id')->on('posts'); 
     $table->integer('tag_id')->unsigned(); 
     $table->foreign('tag_id')->unsigned(); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::drop('post_tag'); 
} 
} 

このエラーを取り除くにはどうすればよいですか、ここで何が欠けていますか?

答えて

1

を移行を実行〜

$table->integer('tag_id')->unsigned(); 
$table->foreign('tag_id')->references('id')->on('tag'); 

重複がありました。

次に、post_tagテーブルを手動で削除します。もう一度作成することができました。また、レコードがある可能性があるので、データベースのmigrationテーブルをチェックすることもできます。 post_tagテーブル。もしそうなら、それを取り除く。その後、安全にマイグレーションを実行できます。

pivot tableを作成しているので、おそらく$table->increments('id');が必要になることはありません。それはあなたの状況に依存する可能性があります。ほとんどの場合、あなたはそれを必要としません。

+0

ありがとう –

1

手動MySQLでのテーブルを削除する必要があります:

mysql> drop table post_tag; 

その後、再び、あなたが

$table->integer('tag_id')->unsigned(); 
$table->foreign('tag_id')->unsigned(); 

を変更する習慣があり、あなたのCreatePostTagTable移行で

php artisan migrate 
+0

エラーが表示されました。[Illuminate \ Database \ QueryException] SQLSTATE [42000]:構文エラーまたはアクセス違反:1064 SQL構文にエラーがあります。行1(SQL:alter table 'post_ tag'追加制約' post_tag_tag_id_foreign'外部キー( 'tag_id')参照' '( ')の近くで') 'の近くで使用する正しい構文については がMariaDBサーバーのバージョンに対応していることを確認してください。 )) ' –

+0

データベースに実動データがなく、プロジェクトが開発中の場合は、データベース全体を削除して作成し、Gayanが次に示唆した変更を行い、その後、PHPの職人をマイグレーションするのが良い – Paras

0

私はあなたがこのしようとすべきだと思う:

Drop table post_tag; 

削除post_tag移行を実行

php artisan migrate 
移行テーブル

から移行を

あなたのためにこの作品が欲しいです!

関連する問題