Php 如何使用CDbMigration从表中删除键
这是我的表的结构:Php 如何使用CDbMigration从表中删除键,php,mysql,yii,foreign-keys,database-migration,Php,Mysql,Yii,Foreign Keys,Database Migration,这是我的表的结构: CREATE TABLE `company_info` ( `id` int(11) NOT NULL, `name` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL, `description` longtext COLLATE utf8_polish_ci, `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TI
CREATE TABLE `company_info` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
`description` longtext COLLATE utf8_polish_ci,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_info` date DEFAULT NULL,
`company_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `Index_1` (`id`),
KEY `FK_Reference_4` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci
我需要更改它,在Yii 1.x中使用cdbmiglation。目前,键FK_Reference_4
(company_id
)是表company
中id
的外键。我正试图:
- 删除旧的foring键
- 向另一个表(
)添加新的用户
yiic migration
执行迁移时,命令失败:
$this->dropForeignKey('FK_Reference_4','company_info');
出现以下错误:
Exception: CDbCommand failed to execute the SQL
statement: SQLSTATE[HY000]: General error: 1025 Error on rename of '.\focuserv\
company_info' to '.\focuserv\#sql2-1b0-18' (errno: 152). The SQL statement execu
ted was: ALTER TABLE `company_info` DROP FOREIGN KEY `FK_Reference_4` (E:\IDEs\w
amp\www\focuserv\focuserv\framework\db\CDbCommand.php:358)
因此,
dropForeignKey
失败了。我应该如何修复或解决此问题?根据上面的create table sql语句,它看起来不像您的FK_Reference\u 4
是一个外键,它看起来像一个普通索引。试着改变
$this->dropForeignKey('FK_Reference_4','company_info');
到
仅供参考,外键sql语句如下所示:
ALTER TABLE `company_info`
ADD CONSTRAINT `FK_Reference_4` FOREIGN KEY (`company_id`)
REFERENCES `company` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
@PitcPitchinnate,谢谢,dropIndex有效。它看起来好像powerdesigner没有进行约束,但我使用了
$this->addForeignKey('FK.'$FKTableName.'to'$refTable,$FKTableName,$FKColumnName,$refTable,$refColumn))代码>和结果:创建表company\u info
(…….KEYFK\u company\u info\u to\u users
(company\u id
),约束FK\u company\u info\u to\u users
),外键(company\u id
)引用用户(id
)…
ALTER TABLE `company_info`
ADD CONSTRAINT `FK_Reference_4` FOREIGN KEY (`company_id`)
REFERENCES `company` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;