简体   繁体   English

当我运行php artisan migration命令时出错

[英]Error when I run php artisan migrate command

I am trying to add a commenting system on my blog application however I get this error when trying to run comment migration which seems to have nothing to do with my current comments migration file, but a previous post_tag migration file 我正在尝试在博客应用程序上添加评论系统,但是尝试运行评论迁移时出现此错误,这似乎与当前的comments迁移文件无关,但与以前的post_tag迁移文件post_tag

[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) 

This is my comments migration file 这是我的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');
}
}

and this is my post_tag migration file 这是我的post_tag迁移文件

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

How do I get rid of this error or what am I missing here? 我如何摆脱这个错误,或者我在这里错过了什么?

You would have to manually drop the table in mysql: 您必须手动将表删除到mysql中:

mysql> drop table post_tag;

Then run the migration again 然后再次运行迁移

php artisan migrate

In your CreatePostTagTable migration you may wont to change 在您的CreatePostTagTable迁移中,您可能不会更改

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

to

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

There was a duplication. 有重复。

Then manually remove post_tag table. 然后手动删除post_tag表。 It could have be created once again. 可以再次创建它。 You may also want to check on your migration table in your database as there could be a record of. 您可能还需要检查数据库中的migration表,因为可能会有记录。 post_tag table. post_tag表。 If so remove it. 如果是这样,将其删除。 Then you could run migration safely. 然后,您可以安全地运行迁移。

Additionally since you are creating a pivot table you don't probably gonna need a $table->increments('id'); 另外,由于您正在创建pivot table ,因此可能不需要$table->increments('id'); . It could be depend on your situation. 这可能取决于您的情况。 In most cases you don't need it. 在大多数情况下,您不需要它。

I think you should try this: 我认为您应该尝试这样做:

Drop table post_tag;

AND

Delete post_tag migration from migrations table 迁移表中 删除post_tag迁移

After run the migration 运行迁移后

php artisan migrate

Hope this work for you! 希望这项工作对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM