【问题标题】:laravel 5 migration error when run newly created mirgration file运行新创建的迁移文件时laravel 5迁移错误
【发布时间】:2016-08-29 08:42:40
【问题描述】:

我刚刚创建了一个新的迁移文件,用于在现有表中插入一个新列。

文件代码为:

<?php

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

class AddStatusToPhoto extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('photos', function(Blueprint $table)
        {
            $table->integer('status');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }


}

但是当我运行 php artisan migrate 时,出现错误消息:

  [Illuminate\Database\QueryException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'permission_
  role' already exists (SQL: create table `permission_role` (`id` int unsigne
  d not null auto_increment primary key, `permission_id` int unsigned not nul
  l, `role_id` int unsigned not null, `created_at` timestamp default 0 not nu
  ll, `updated_at` timestamp default 0 not null) default character set utf8 c
  ollate utf8_unicode_ci)

有人知道是什么问题吗?

【问题讨论】:

  • 在您的permission_role迁移脚本中,必须在down()中添加drop table?
  • 我注意到permission_role表没有回滚,Aldo我手动删除它时,同样的错误仍然存​​在。

标签: php laravel laravel-5


【解决方案1】:

您之前的迁移角色只运行了半成功。这意味着表permission_role 已添加,但提前终止。因此,迁移从未被标记为完成并添加到migrations 表中。它尝试重新运行权限迁移文件,但由于表已经存在而失败。您要么需要手动将旧迁移文件添加到数据库中,要么使其成功运行。

可以删除代码,或将其包装在 if 语句中。
Is there any way to detect if a database table exists with Laravel

【讨论】:

    【解决方案2】:

    确保该表实际上并不存在于数据库中,并且 down() 方法应包含能够删除该表的代码。

    【讨论】:

    • 这应该是评论
    【解决方案3】:

    在使用命令"php artisan migrate:refresh"之前,您应该首先使用命令“php artisan migrate”将迁移原始文件记录在迁移表中,并且您还需要更正向下方法。

    migrate:refresh 命令将首先回滚所有数据库迁移,然后运行 ​​migrate 命令。此命令有效地重新创建整个数据库:

    php artisan migrate:refresh
    
    php artisan migrate:refresh --seed
    

    请参阅此documentation

    【讨论】:

    • 我尝试过刷新但不起作用(我还记得会删除所有数据吗?),最后我暂时删除了所有旧迁移文件
    【解决方案4】:

    将这行代码放入您的迁移中

    if(!Schema::hasTable('post')){
      Schema::create('post', function (Blueprint $table) {
                $table->increments('id');
                #etc
            });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2020-10-24
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 2016-10-23
      • 2015-07-06
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多