简体   繁体   中英

sql query run successfully in mysql, but not in yii migration

I'm a newbie to yii and have found a problem when executing a migration file. Below is the migration code (sorry the field name are in Indonesian:

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;
}

Every time I tried to execute this migration, it is always failed with errors:

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

But when I execute those two lines of sql query in mysql, it run successfully. What is wrong with the migration file? Can anybody help me? I appreciate it.

Try:

 $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'); 

I don't know what wrong with your sql, maybe in one execution must do one action, but my code must work. Don't forget return true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM