Voglio cambiare il tipo di dati di alcune colonne della chiave primaria nel mio database da INT a BIGINT. La seguente definizione è un giocattolo-esempio per illustrare il problema:mysql altera int colonna in bigint con chiavi esterne
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`);
Ora quando provo a execut uno dei seguenti comandi:
ALTER TABLE `thing` CHANGE `id` `id` BIGINT NOT NULL AUTO_INCREMENT;
ALTER TABLE `owner` CHANGE `thing_id` `thing_id` BIGINT NOT NULL;
sto correndo in un errore
#1025 - Error on rename of './debug/#[temp-name]' to './debug/[tablename]' (errno: 150)
MOSTRA uscite INODB Stato:
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")
Suppongo che la definizione di chiave esterna blocchi il cambiamento del tipo di colonna su entrambi i lati. L'approccio ingenuo per risolvere questo problema sarebbe quello di eliminare le definizioni di chiavi esterne, modificare le colonne e ridefinire le chiavi esterne. C'è una soluzione migliore?
L'approccio che hai citato non è ingenuo, puoi davvero usarlo. – Devart
Grazie Devart! Stavo cercando una soluzione meno dolorosa poiché devo farlo su molti tavoli, ma a quanto pare cadere e ricreare i vincoli sembra essere la strada da percorrere: - /. – deif