【问题标题】:Yii framework: migrations doesn't create/drop tablesYii 框架:迁移不会创建/删除表
【发布时间】:2013-09-24 08:43:36
【问题描述】:

尝试使用 Yii 迁移删除一个表并创建 2 个。 这是代码:

    <?php

class m130919_095159_create_offer_tables extends CDbMigration
{
    public function up()
    {
        $this->getDbConnection()->createCommand()->dropTable('offer');

        $this->createTable('settings', array(
            'id' => 'pk',
            'offer_id' => 'integer NOT NULL',
            'system_id' => 'integer NULL',  
            'site_title' => 'varchar(255) NULL',
            'index_number' => 'integer NULL',  
            'coupon_token' => 'varchar(255) NULL',
            'facebook_app_id' => 'varchar(255) NULL',
            'facebook_app_secret' => 'varchar(255) NULL',
        ));

        $this->createTable('content', array(
            'id' => 'pk',
            'offer_id' => 'integer NOT NULL',
            'created' => 'datetime NOT NULL',
            'modified' => 'datetime NOT NULL',
            'status' => 'integer NOT NULL',
            'title_uz' => 'varchar(255) NULL',
            'title_ru' => 'varchar(255) NULL',
            'description_uz' => 'text NULL',
            'description_ru' => 'text NULL', 
        ));
    }

    public function down()
    {
        echo "m130919_095159_create_offer_tables does not support migration down.\n";
        return false;
    }
}

在我执行命令php yiic.php migrate 并得到答案migrated up successfully 之后。问题是 sql 命令尚未执行。表未删除,未创建其他表。数据库没有变化。找不到原因

【问题讨论】:

    标签: php database web yii database-migration


    【解决方案1】:

    你以错误的方式使用迁移。正确的做法是无论你在 up 函数中做什么,只要尝试在 down 函数中做相反的事情。

    假设你在 up 函数中删除一个表并创建两个表,那么你应该在 down 函数中编写代码

    为您在 up 中创建的表创建表代码并为您在 up 中创建的两个表创建表代码

    首先在console.php中检查你的数据库配置

    public function up() {
      $this->dropTable('offer');
      $this->createTable('settings', array(
          'id' => 'pk',
          'offer_id' => 'integer NOT NULL',
          'system_id' => 'integer NULL',  
          'site_title' => 'varchar(255) NULL',
          'index_number' => 'integer NULL',  
          'coupon_token' => 'varchar(255) NULL',
          'facebook_app_id' => 'varchar(255) NULL',
          'facebook_app_secret' => 'varchar(255) NULL',
      ));
       $this->createTable('content', array(
          'id' => 'pk',
          'offer_id' => 'integer NOT NULL',
          'created' => 'datetime NOT NULL',
          'modified' => 'datetime NOT NULL',
          'status' => 'integer NOT NULL',
          'title_uz' => 'varchar(255) NULL',
          'title_ru' => 'varchar(255) NULL',
          'description_uz' => 'text NULL',
          'description_ru' => 'text NULL', 
      ));
    }
    
    public function down(){
      $this->createTable('offer', array(
        //attributes for offer table
      ));
      $this->dropTable('settings');
      $this->dropTable('content');
    } 
    

    【讨论】:

    • 是的,你说得对,问题是console.php中的数据库配置。非常感谢,Neophile
    • 鸭子!花了 30 分钟看魔术,结果发现它写入 sql db 时对我的 mysqldb 没有任何影响。
    【解决方案2】:

    您必须在 config 目录下的 main.php 和 console.php 上正确配置数据库。如果您使用 MySQL,请取消注释该部分并在 sqlite 下注释 testdrive.db。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2019-01-08
      • 2013-08-10
      • 2020-12-01
      • 2014-08-23
      相关资源
      最近更新 更多