Yii迁移dropTable和FK

时间:2013-07-11 12:16:46

标签: migration yii

我有迁移:

功能Up:

$this->createTable(
            'vacancy_moderate',
            array(
                'id' => 'int(10) unsigned NOT NULL AUTO_INCREMENT',
                'period_days' => 'tinyint(4) NOT NULL',
                'title' => 'varchar(255) NOT NULL DEFAULT \'\'',
                'price'=> 'int(11) DEFAULT NULL',
                'requirements'=> 'text NOT NULL',
                'conditions'=> 'text',
                'contact_details'=> 'text NOT NULL',
                'country_id'=> 'int(10) unsigned NOT NULL',
                'city_id'=> 'int(10) unsigned DEFAULT NULL',
                'user_id'=> 'int(10) unsigned DEFAULT NULL',
                'club_id'=> 'int(10) DEFAULT NULL',
                'PRIMARY KEY (`id`)',
                'KEY `city_id` (`city_id`)',
                'KEY `user_id` (`user_id`)',
                'KEY `country_id` (`country_id`)',
                'KEY `club_id` (`club_id`)',
                'CONSTRAINT `vacancy_moderate_ibfk_1` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_3` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_4` FOREIGN KEY (`club_id`) REFERENCES `clubs` (`id`) ON UPDATE CASCADE'
            ),
            'ENGINE=InnoDB DEFAULT CHARSET=utf8'
        );

功能向下:

$this->dropTable('vacancy_moderate');

问题: 我应该手动删除所有外键还是将它们放在dropTable中?

2 个答案:

答案 0 :(得分:1)

随着一切消失,他们会随桌摆放。 : - )

答案 1 :(得分:0)

Yii dropTable()函数只调用MySQL命令“DROP TABLE {table name}”。当您删除表时,MySQL将删除外键。

public function dropTable($table)
{
    echo "    > drop table $table ...";
    $time=microtime(true);
    $this->getDbConnection()->createCommand()->dropTable($table);
    echo " done (time: ".sprintf('%.3f', microtime(true)-$time)."s)\n";
}