2016-05-04 51 views
2

我剛剛創建了一個新的遷移文件,爲現有表插入一個新列。laravel 5運行新創建的鏡像文件時出現遷移錯誤

文件代碼:

<?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的工匠遷移,有一個錯誤信息:

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

人知道是什麼問題?

+0

在你permission_role遷移腳本,不得不在屋裏'向下()'添加刪除表? –

+0

我注意到permission_role表沒有回滾,Aldo當我手動刪除它,同樣的錯誤仍然存​​在。 –

回答

0

您以前的遷移角色只能半成功運行。這意味着表permission_role已被添加,但提前終止。因此,遷移永遠不會被標記爲完整並添加到migrations表中。它嘗試重新運行權限遷移文件,但因表已存在而失敗。您可能需要手動將舊遷移文件添加到數據庫,或者使其成功運行。

可以刪除代碼或將其包裝在if語句中。
Is there any way to detect if a database table exists with Laravel

0

確保該表在物理上不存在,並且down()方法應該包含能夠刪除該表的代碼。

+0

這應該是評論 – ketan

0

在使用命令"php artisan migrate:refresh"之前,您應該首先使用命令「php artisan migrate」在遷移表中記錄遷移原始文件,並且還需要更正down方法。

migrate:refresh命令將首先回滾所有數據庫遷移,然後運行migrate命令。該命令有效地重新創建你的整個數據庫:

php artisan migrate:refresh 

php artisan migrate:refresh --seed 

轉寄此documentation

+0

i'had嘗試刷新但不工作(我還記得將刪除所有數據?),最後我刪除所有舊的遷移文件臨時 – hkguile

0

,就把這行代碼在你遷移

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