sql查询在mysql中成功运行,但在yii迁移中没有

时间:2013-11-01 14:14:51

标签: sql yii migration

我是yii的新手,在执行迁移文件时发现了问题。下面是迁移代码(抱歉,字段名称是印度尼西亚语:

public function safeUp()
{
    $this->execute("
        ALTER TABLE `t_retur_pembelian` ALTER `kode_pembelian` DROP DEFAULT;
        ALTER TABLE `t_retur_pembelian` CHANGE COLUMN `kode_pembelian` `kode_post_pembelian` VARCHAR(16) NOT NULL AFTER `kode`, DROP FOREIGN KEY `FK_T_PEMBELIAN_T_RETUR_PEMBELIAN`, ADD CONSTRAINT `FK_t_retur_pembelian_t_post_pembelian` FOREIGN KEY (`kode_post_pembelian`) REFERENCES `t_post_pembelian` (`kode`) ON UPDATE NO ACTION ON DELETE NO ACTION;
    ");
}

public function safeDown()
{
    return false;
}

每次我尝试执行此迁移时,它总是因错误而失败:

...Exception: CDbCommand failed to execute the SQL statement: SQLSTATE[42000] [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE `t_retur_pembelian` CHANGE COLUMN `kode_pembelian` `kode_post_pembel' at line 2. The SQL statement executed was: ALTER TABLE `t_retur_pembelian` ALTER `kode_pembelian` DROP DEFAULT;

但是当我在mysql中执行这两行sql查询时,它运行成功。迁移文件有什么问题?有谁能够帮我?我很感激。

1 个答案:

答案 0 :(得分:0)

尝试:

$this->execute("ALTER TABLE `t_retur_pembelian` ALTER `kode_pembelian` DROP DEFAULT");
$this->renameColumn('t_retur_pembelian', 'kode_pembelian', 'kode_post_pembelian VARCHAR(16) NOT NULL AFTER `kode`');
$this->dropForeignKey('FK_T_PEMBELIAN_T_RETUR_PEMBELIAN', 't_retur_pembelian');
$this->addForeignKey('FK_t_retur_pembelian_t_post_pembelian', 't_retur_pembelian', 'kode_post_pembelian', 't_post_pembelian', 'kode', 'NO ACTION', 'NO ACTION');

我不知道你的sql有什么问题,也许在一次执行中必须做一个动作,但我的代码必须工作。不要忘记return true;