【问题标题】:Yii2 : Drop table using migrationYii2:使用迁移删除表
【发布时间】:2015-12-22 05:42:23
【问题描述】:

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

这是我的迁移课程:

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]:

如果我输入“yes”,那么它会恢复两者,但我只想删除 m130524_201442_init。

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

【问题讨论】:

  • 请显示错误信息。
  • 不要运行migrate/down 130524_201442。运行 migrate/down 1 之类的东西来恢复最后一个迁移,migrate/down 2 来恢复最后两个,等等。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个:

    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....

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 迁移按时间顺序上升/下降
    猜你喜欢
    • 2011-10-29
    • 2017-02-23
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2013-06-11
    • 2015-10-07
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多