2015-12-22 56 views
1

我想使用遷移類方法刪除表。Yii2:使用遷移刪除表

這是我的移民類:

use yii\db\Schema; 
use yii\db\Migration; 

class m130524_201442_init extends Migration 
{ 
    public function up() 
    { 
     $tableOptions = null; 
     if ($this->db->driverName === 'mysql') { 
      // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci 
      $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; 
     } 

     $this->createTable('{{%user}}', [ 
      'id' => Schema::TYPE_PK, 
      'username' => Schema::TYPE_STRING . ' NOT NULL', 
      'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL', 
      'password_hash' => Schema::TYPE_STRING . ' NOT NULL', 
      'password_reset_token' => Schema::TYPE_STRING, 
      'email' => Schema::TYPE_STRING . ' NOT NULL', 
      'firstname' => Schema::TYPE_STRING, 
      'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', 
      'created_at' => Schema::TYPE_INTEGER . ' NOT NULL', 
      'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL', 
     ], $tableOptions); 
    } 

    public function down() 
    { 
     $this->dropTable('{{%user}}'); 
    } 
} 

我遷移使用警予遷移/這個類130524_201442。從那以後,我做了很多其他的遷移,現在我想刪除用戶表,所以我嘗試下面的命令,

yii migrate safeDown console\migrations\m130524_201442_init.php 

yii migrate down console\migrations\m130524_201442_init.php 

OUTPUT:

D:\Projects\wamp\www\yiiadvanced>yii migrate down console\migrations\m130524_201442_init.php 
Yii Migration Tool (based on Yii v2.0.5) 

No new migration found. Your system is up-to-date. 

也試過這樣:

yii migrate/down 130524_201442 

OUTPUT

D:\Projects\wamp\www\yiiadvanced>yii migrate/down 130524_201442 
Yii Migration Tool (based on Yii v2.0.5) 

Total 2 migrations to be reverted: 
     m151222_063506_roles 
     m130524_201442_init 

Revert the above migrations? (yes|no) [no]: 

如果我輸入「是」,那麼它將恢復兩個,但我只想刪除m130524_201442_init。

那麼,我應該使用哪個命令?

+0

請,顯示錯誤消息。 – Psyhos

+0

不要運行'migrate/down 130524_201442'。運行'migrate/down 1'來恢復最後一次遷移,'migrate/down 2'恢復最後兩次遷移等。 – Beowulfenator

回答

0

試試這個:

public function up() 
{ 
    $this->createTable('user', [ 
     'id' => Schema::TYPE_PK, 
     'username' => Schema::TYPE_STRING . ' NOT NULL', 
     'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL', 
     'password_hash' => Schema::TYPE_STRING . ' NOT NULL', 
     'password_reset_token' => Schema::TYPE_STRING, 
     'email' => Schema::TYPE_STRING . ' NOT NULL', 
     'firstname' => Schema::TYPE_STRING, 
     'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', 
     'created_at' => Schema::TYPE_INTEGER . ' NOT NULL', 
     updated_at' => Schema::TYPE_INTEGER . ' NOT NULL', 
     ], $tableOptions); 
} 

public function Down() 
{ 
    $this->dropTable('user'); 
} 

更多信息click here....

+0

檢查編輯。是的,我檢查了這個鏈接。請參閱 - http://www.yiiframework.com/doc-2.0/guide-db-migrations.html#drop-table。但它是說我必須爲drop table創建新類。但我想用現有的。 – Napster

+0

不,你不需要另一個班級放棄表格,在我的代碼中它正常工作。 –

+0

請讓我知道通過哪個命令? – Napster

0

看來,你跑錯了命令。請顯示您收到的錯誤消息。

在控制檯中轉至警予文件所在,並嘗試目錄: php yii migrate/to m130524

+0

請檢查編輯 – Napster

+0

遷移按時間順序上升/下降 – Psyhos

0

可能這是硬的方式,但它只是對我。

  • 使用phpmyadmin或其他工具手動刪除表。
  • 根據需要更新您的遷移文件。
  • 默認情況下,在yii數據庫中有migration表,該表 存儲您正在開發模式下進行的所有遷移的歷史記錄。在version列 刪除值是有同名 遷移文件