2

这是我的表的结构:

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 中使用 CDbMigration。目前, key FK_Reference_4( ) 是tablecompany_id的外键。我试着:idcompany

  • 删除旧的外键,
  • 将新表添加到另一个表 ( users)。

当我尝试执行迁移时,使用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失败也是如此。我应该如何修复或解决此问题?

4

1 回答 1

2

根据您上面的 create table sql 语句,它看起来不像FK_Reference_4是外键,它看起来像普通索引。尝试改变

$this->dropForeignKey('FK_Reference_4','company_info');

$this->dropIndex('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;
于 2013-10-24T14:36:54.017 回答