2017-09-26 76 views
0

我是laravel的新手。我創建了遷移,我可以在我的數據庫/遷移文件夾中看到它們。但是,當我運行php artisan migrate命令時,它拋出這個錯誤:Laravel Migrations

[PDOException]                   
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists 

是的,我已經遷移的用戶表了,但現在我已經創造了另一個遷移。是不是應該遷移我在php artisan migrate命令後創建的最後一次遷移?

+0

有沒有在你的數據庫了'migrations'表。它應該創建該表並使用它來跟蹤哪些遷移已經運行。 – jfadich

+0

您是否嘗試過PHP工匠遷移:刷新或PHP工匠遷移:復位或PHP工匠遷移:回滾--step = 5 && PHP工匠遷移:刷新? – Andy

回答

1

如果您的用戶表第二個遷移,看起來像這樣:

Schema::create('users', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('name', 25); 
    // more migrations ... 
}); 

它會嘗試再次創建users表。請注意第二次遷移有Schema::創建。您希望做更多的東西一樣改變表:

Schema::table('users', function (Blueprint $table) { 
    $table->string('name', 50)->change(); 
}); 

如果您正在尋找完全覆蓋(這將刪除表中所有數據)你之前的遷移與新的表,你可以做一些如如下:

<?php 

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

class CreateUsersTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::dropIfExists('users'); 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name', 50); 
      $table->timestamps(); 
     }); 
    } 

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

但是,如果你這樣做,你很可能會誤解遷移的工作原理。目的是爲了能夠在不必完全銷燬表的情況下更改數據庫中的列。