2012-01-26 18 views
12

Quiero cambiar el tipo de datos de algunas columnas de clave primaria en mi base de datos de INT a BIGINT. La siguiente definición es un juguete-ejemplo para ilustrar el problema:mysql alterar columna int a letra grande con claves externas

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

Ahora cuando intento Execut uno de los siguientes comandos:

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

estoy corriendo en un error

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

VER salidas INODB ESTADO:

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

Supongo que la definición de clave foránea bloquea el cambio del tipo de columna en cualquier lado. El enfoque ingenuo para resolver este problema sería eliminar las definiciones de clave foránea, alterar las columnas y redefinir las claves foráneas. hay una mejor solución?

+1

El enfoque que mencionaste no es ingenuo, realmente puedes usarlo. – Devart

+0

Gracias Devart! Estaba buscando una solución menos dolorosa, ya que tengo que hacerlo en muchas mesas, pero aparentemente dejar caer y volver a crear restricciones parece ser el camino a seguir: - /. – deif

Respuesta

7

Incluso con SET foreign_key_checks = 0, no puede modificar el tipo de columna de restricción. Desde MySQL doc: 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.

Por lo tanto, estoy de acuerdo con el comentario de Devart. Simplemente colócalo y créalo de nuevo.

+0

intenté deshabilitar también las comprobaciones de claves externas y no funcionó. gracias por señalar el motivo. – deif

3

que podría sugerir que cambiar el nombre de estos campos en herramienta de interfaz gráfica de usuario - dbForge Studio for MySQL (edición de prueba gratis):

Simplemente seleccione el campo que desea cambiar el nombre de la base de datos en el Explorador, haga clic en Refactoring-> Cambiar el nombre de comando, introduzca nueva nombre en la ventana abierta, y presione OK, cambiará el nombre del campo y volverá a crear todas las claves foráneas de forma automática.

0

tuve un problema similar que la solución es la cláusula de cambio:

ALTER TABLE table_name CHANGE id id BIGINT(20) NOT NULL AUTO_INCREMENT; 

que trabajó para mí.

Cuestiones relacionadas