mysql-alter-int Spalte vom Datentyp bigint mit fremden Federn

Möchte ich ändern Sie den Datentyp einige primary-key-Spalten in meiner Datenbank von INT auf BIGINT. Die folgende definition ist ein Spielzeug-Beispiel um das problem zu veranschaulichen:

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`);

Wenn ich jetzt versuche zu execut Sie einen der folgenden Befehle:

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

hab ich, läuft in einen Fehler

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

ZEIGEN INODB STATUS Ausgänge:

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")

Ich vermute, dass der foreign key-definition Blöcke ändern der Spalte Typ auf beiden Seiten. Der naive Ansatz, um dieses problem zu lösen, wäre zum löschen der Fremdschlüssel Definitionen, ändern Sie die Spalten-und re-definieren Sie den Fremdschlüssel. gibt es eine bessere Lösung?

Der Ansatz, den Sie erwähnt haben, ist nicht naiv, Sie wirklich benutzen kann.
Dank Red Lizard! Ich war auf der Suche für eine weniger schmerzhafte Lösung, da ich auf eine Menge von Tabellen, aber anscheinend zu löschen und neu zu erstellen, Einschränkungen scheint der Weg zu gehen :-/.

InformationsquelleAutor deif | 2012-01-26

Schreibe einen Kommentar