13

データベース内のいくつかの主キー列のデータ型をINTからBIGINTに変更したいと思います。次の定義は、問題を説明するためのおもちゃの例です。

CREATE TABLE IF NOT EXISTS `owner` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `thing_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `thing_id` (`thing_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `thing`;
CREATE TABLE IF NOT EXISTS `thing` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

ALTER TABLE `owner`
  ADD CONSTRAINT `owner_ibfk_1` FOREIGN KEY (`thing_id`) REFERENCES `thing` (`id`);

次のコマンドのいずれかを実行しようとすると、次のようになります。

ALTER TABLE `thing` CHANGE `id` `id` BIGINT NOT NULL AUTO_INCREMENT;
ALTER TABLE `owner` CHANGE `thing_id` `thing_id` BIGINT NOT NULL;

エラーが発生しています

#1025 - Error on rename of './debug/#[temp-name]' to './debug/[tablename]' (errno: 150)

SHOW INODB STATUS出力:

LATEST FOREIGN KEY ERROR
------------------------
120126 13:34:03 Error in foreign key constraint of table debug/owner:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
 CONSTRAINT "owner_ibfk_1" FOREIGN KEY ("thing_id") REFERENCES "thing" ("id")

外部キー定義は、どちらかの側の列タイプの変更をブロックしていると思います。この問題を解決するための単純なアプローチは、外部キー定義を削除し、列を変更して、外部キーを再定義することです。より良い解決策はありますか?

4

4 に答える 4

7

であってもSET foreign_key_checks = 0、制約列の型を変更することはできません。MySQL ドキュメントから: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

However, even if foreign_key_checks = 0, InnoDB does not permit the creation of a foreign key constraint where a column references a nonmatching column type.

だから、私はDevartのコメントに同意します。ドロップして、もう一度作成してください。

于 2012-01-26T15:47:39.080 に答える
3

GUIツールでそのようなフィールドの名前を変更することをお勧めします-dbForge Studio for MySQL(無料試用版):

データベース エクスプローラーで名前を変更するフィールドを選択し、[リファクタリング] -> [名前の変更] コマンドをクリックし、開いたウィンドウに新しい名前を入力して [OK] を押すだけで、フィールドの名前が変更され、すべての外部キーが自動的に再作成されます。

于 2012-01-26T14:50:00.673 に答える