2017-02-27 95 views
0

我已經將名稱爲im_useless的列插入到我的表中,而我之前不需要該列。Laravel 5 - 使用migrate:回滾路徑

這是我的架構(文件名:2017_02_27_120313_units.php):

public function up() 
{ 
    Schema::create('units', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('description'); 
     $table->string('im_useless'); 
     $table->timestamps(); 
    }); 
} 

現在我嘗試刪除它,所以我用了down()函數內部的代碼:

public function down() 
{ 
    Schema::dropColumn('im_useless'); 
} 

新模式:

public function up() 
{ 
    Schema::create('units', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('description'); 
     $table->timestamps(); 
    }); 
} 

現在我必須回滾然後再次遷移。我嘗試通過執行php artisan help migrate:rollback僅回滾該特定的遷移文件,我發現有一個--path選項。

於是,我就回滾像這樣的特定遷移:

php artisan migrate:rollback --path=2017_02_27_120313_units.php 

,但我得到Nothing to rollback

我怎麼能丟棄,而不必回滾任何其他遷移特定的列?


UPDATE:

我想我必須改變path這樣的:

php artisan migrate:rollback --path=database/migrations/2017_02_27_120313_units.php 

...因爲我的php殼是在項目的根文件夾中打開?

但是我仍然得到Nothing to rollback

我也試過php artisan migrate --path=2017_02_27_120313_units.phpphp artisan migrate --path=database/migrate/2017_02_27_120313_units.php

...並獲得Nothing to migrate


更新2

我想我已經搞砸我的遷移表,因爲我刪除了down()函數中的代碼,並且該表從未被刪除。 https://stackoverflow.com/a/26077506/4684797

+0

使用遷移不僅僅是直接做業務數據庫如要複雜得多與phpmyadmin ...多數民衆贊成我今天所學 – Black

回答

2

回滾函數旨在讓您可以恢復到您遷移前的版本,以防部署時出現問題。如果您想刪除不再需要的特定列,則應該將其視爲新的遷移,並將該列放入up()方法中。

+0

無法正常工作。如果我嘗試像這樣'php artisan make:migrate units --create = units --table = units'做一個新的遷移,那麼我會得到'InvalidArgumentException,一個單位遷移已經存在' – Black

+0

'php artisan make:migration dropColumnFromTable'用'public function up()'方法,'Schema :: table(「table」,function(...){Schema :: dropColumn(「column 「);});'。 'down'函數可以設置爲將'column'添加回'table',但也可以(也可能應該)留空。 –

-1

運行作曲家轉儲自動加載,然後再試一次

+0

已經嘗試過。 downvote不是從我btw。 – Black

+0

好吧,我認爲你必須在Schema :: table('units',function(Blueprint $ table){Schema :: dropColumn('im_useless');} – AhmedAlawady

+0

裏面添加dropColumn也不行,同樣的錯誤'InvalidArgumentException,A Units遷移已經存在「 – Black