Yii Framework - yic migrate command doesn't wo

2019-09-06 18:15发布

问题:

I am trying to create a table using Yii db migration in safeUp() method. However, when I use the "./yiic migrate" command, it runs successfully but the table doesn't appear in the database. The following is code from the migration file:

<?php

class m130808_123826_test_table extends CDbMigration
{

    public function up()
{
}

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

public function safeUp()
{
   $this->createTable('tbl_test', array(
   'test_field1' => 'int(10)',
   'test_field2' => 'string NOT NULL',
    ), 'ENGINE=InnoDB');
}

public function safeDown()
{
   $this->dropTable('tbl_test');
}

}

I tried using a few suggestions from comments on similar issue like, providing 'up' or 'safeUp' parameter to the migrate command. I also checked the console.php file and it is configured to the right database. What else could be the reason for not creating the table in the database?

回答1:

You should provide either the up or safeUp method. So remove the empty up method in your class:

<?php

class m130808_123826_test_table extends CDbMigration
{

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

public function safeUp()
{
   $this->createTable('tbl_test', array(
   'test_field1' => 'int(10)',
   'test_field2' => 'string NOT NULL',
    ), 'ENGINE=InnoDB');
}

public function safeDown()
{
   $this->dropTable('tbl_test');
}

}