Ho una tabella in MySQL InnoDB creato così:MySQL - non può aggiungere estera chiave
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`type` enum('MANUAL','FACEBOOK') NOT NULL DEFAULT 'MANUAL',
`role` enum('COOK','HOST','ALL') NOT NULL DEFAULT 'ALL',
`about_me` varchar(1000) DEFAULT NULL,
`food_preferences` varchar(1000) DEFAULT NULL,
`cooking_experience` varchar(1000) DEFAULT NULL,
`with_friend` bit(1) DEFAULT b'0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
Poi ho provato ad aggiungere una tabella con una tale affermazione (non le chiavi esterne aggiunto durante la creazione della tabella come avuto problemi con che):
CREATE TABLE `messages` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`from` bigint(20) NOT NULL,
`to` bigint(20) NOT NULL,
`content` varchar(10000) NOT NULL,
`timestamp_sent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`timestamp_read` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Avendo tabelle create, ho bisogno di aggiungere, infine, chiavi esterne su 'from'
e 'to'
campi che fanno riferimento 'users'.'id'
campo:
ALTER TABLE `messages`
ADD CONSTRAINT `messages_users_fk`
FOREIGN KEY (`from` , `to`)
REFERENCES `users` (`id` , `id`)
ON DELETE SET NULL
ON UPDATE CASCADE
, ADD INDEX `messages_users_fk_idx` (`from` ASC, `to` ASC)
L'errore che ottengo è:
ERROR: Error 1822: Failed to add the foreign key constraint. Missing index for constraint 'messages_users_fk' in the referenced table 'users'.
Ma tabella 'utenti' ha un indice PRIMARY
su 'id'
...
cercato anche di fare un passo più piccolo e aggiungere chiave esterna solo per 'from'
campo :
ALTER TABLE `messages`
ADD CONSTRAINT `messages_users_fk`
FOREIGN KEY (`from`)
REFERENCES `users` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
, ADD INDEX `messages_users_fk_idx` (`from` ASC) ;
L'errore è leggermente diverso:
01.235.164,106174 millionsI tipi di campi sono gli stessi (bigint(20) NOT NULL
) come è stato suggerito come causa del problema in altri thread StackOverflow. Le mie tabelle non sono partizionate (il manuale MySQL lo afferma come una limitazione per avere vincoli di chiave esterna in InnoDB). La tabella 'messages'
non memorizza alcuna riga al momento, quindi i dati memorizzati non possono essere il problema in alcun modo. Sono bloccato, per favore aiuto.
+1 Bello vedere le istruzioni effettive 'CREATE TABLE' per giocare con loro :) –