Yii2:使用迁移删除表

时间:2015-12-22 05:42:23

标签: php yii2 database-migration yii2-advanced-app

我想使用迁移类方法删除表。

这是我的迁移类:

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}}');
    }
}

我使用 yii migrate / to 130524_201442 迁移了此类。之后我做了很多其他的迁移,现在我想删除用户表,所以我尝试了下面的命令,

yii migrate safeDown console\migrations\m130524_201442_init.php

yii migrate down console\migrations\m130524_201442_init.php

输出:

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

输出

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。

那么,我应该使用哪个命令?

3 个答案:

答案 0 :(得分: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....

答案 1 :(得分:0)

看来,你运行错误的命令。请显示您收到的错误消息。

在控制台中,转到yii文件所在的目录并尝试: php yii migrate/to m130524

答案 2 :(得分:0)

可能这很难,但对我有用。

  • 使用phpmyadmin或其他工具手动删除表格。
  • 根据需要更新您的迁移文件。
  • 默认情况下,在yii数据库中有migration表,此表 存储您在开发模式下进行的所有迁移的历史记录。 删除version列中具有相同名称的值 迁移文件