尝试使用 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 命令尚未执行。表没有删除,另一个表没有创建。数据库中没有更改。找不到原因
您以
错误的方式使用迁移。正确的方法是,无论你在向上函数中做什么,只要尝试在向下函数中做相反的事情。
假设在这里你删除了一个表并在 up 函数中创建两个表,那么你应该在 down 函数中编写代码
为要放入的表创建表代码,并为在 Up 中创建的两个表创建表代码
首先在控制台中检查数据库配置.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');
}
您必须
在主.php和控制台上正确配置数据库.php在配置目录下。如果您使用的是 MySQL,请取消注释该部分并注释测试驱动器.db在 sqlite 下。