Cannot add foreign key constraint

JackIlPazzo

Utente Attivo
25 Lug 2014
69
0
6
Ciao,

sto cercando di associare come chiave esterna il GUID della tabella "service_categories" con questa query:

Codice:
ALTER TABLE `services`
  ADD CONSTRAINT `services_ibfk_1` FOREIGN KEY (`guid_service_categories`) REFERENCES `service_categories` (`GUID`) ON DELETE SET NULL ON UPDATE CASCADE;

Ecco la struttura della tabella services:

Codice:
CREATE TABLE IF NOT EXISTS `services` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `GUID` char(36) NOT NULL UNIQUE,
  `name` varchar(256) DEFAULT NULL,
  `duration` int(11) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `currency` varchar(32) DEFAULT NULL,
  `description` text,
  `guid_service_categories` char(36) NOT NULL,
  `data` int(11) NOT NULL,
  `lastUpdated` varchar(36),
  PRIMARY KEY (`id`),
  KEY `guid_service_categories` (`guid_service_categories`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Ed ecco quella di service_categories

Codice:
CREATE TABLE IF NOT EXISTS `service_categories` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `GUID` char(36) NOT NULL UNIQUE,
  `name` varchar(256) DEFAULT NULL,
  `description` text,
  `data` int(11) NOT NULL,
  `lastUpdated` varchar(36),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Ottengo però il seguente messaggio di errore:

Cannot add foreign key constraint

Quando eseguo l'alter... Cosa sto sbagliando?
 

Sergio Unia

Utente Attivo
7 Gen 2016
30
1
8
Todi
www.sunelweb.net
Ciao,
credo che il problema sia questo :
ON DELETE SET NULL significa che il campo guid_service_categories della tabella services deve contenere null quando un record corrispondente nell'altra tabella viene cancellato, ma questo non sarebbe possibile perché quel campo ha l'attibuto not null.
In pratica prima di costruire la FK dovresti eseguire una query come questa:

ALTER TABLE `services` CHANGE `guid_service_categories` `guid_service_categories` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL ;

oppure modificare la CREATE TABLE.

Spero di esserti stato utile, buona serata.

s.
 
Discussioni simili
Autore Titolo Forum Risposte Data
L Error Code: 1215. Cannot add foreign key constraint MySQL 3
M non static method cannot be referenced from a static context Java 1
M Errore pagina php 'Cannot detect primary key' PHP 8
G Errore Cannot read property 'childNodes' of undefined per mancanza nodo nel file xml XML 6
romeocharly [WordPress] .../ffmpeg/ffmpeg32.so: cannot execute binary file" WordPress 11
G [PHP] Warning: Cannot modify header information - headers already sent by PHP 3
A Errore: cannot call methods on dialog prior to initialization; attempted to call method 'open' jQuery 4
J Errore the value cannot be null or empty ASP.NET 1
Nik Cannot send session cache limiter - ERRORE utilizzando session_start(); PHP 3
L Error: int cannot be dereferenced Java 1
filippino Fatal error: Cannot redeclare (function) PHP 2
L Cannot find executable for CFBundle Sviluppo app per iOS 0
L ITSM-9000 the bundle identifier cannot be changed from the current value Sviluppo app per iOS 0
D Cannot find XELDZ in all drives Windows e Software 0
B Uncaught TypeError: Cannot read property Javascript 7
H Warning: Cannot modify header information... PHP 6
T Fatal error: Cannot access empty property PHP 2
A errore : "550 cannot stor no permission" per pubblicare il sito con expression web Webdesign e Grafica 0
A errore : "550 cannot stor no permission" per pubblicare il sito con expression web HTML e CSS 1
L Warning: Cannot modify header information - headers already sent by... PHP 9
H Warning: session_start() [function.session-start]: Cannot send session cache limiter PHP 8
M Warning: session_start(): Cannot send session cache... ???? PHP 5
M Warning: Cannot modify header information - headers already sent by (output started a PHP 1
I Cannot access a closed file ASP.NET 2
R Come creare sistema Add to homescreen PHP 3
F add box top page HTML e CSS 9
A Aprire la window form Add Connection da codice Visual Basic 4
R How to add text watermark direct on the HTML page? HTML e CSS 1
bubino8 [Javascript] Resize add/remove Class Javascript 1
F Add expires headers SEO e Posizionamento 3
webmachine [PRESTASHOP] Add pagamento con carte E-Commerce 1
P non trovo l'add-ons HDR options che ho appena installato Photoshop 1
R pulsante "Add" Sviluppo app per Android 2
P Supporto per creare uno script "Add Cart" Offerte e Richieste di Lavoro e/o Collaborazione 7
J problema jquery add/removeclass jQuery 1
F [VENDO] Script portale annunci + svariati add-on (il più serio in circolazione) Offerte e Richieste di Lavoro e/o Collaborazione 0
J Top 10 Free Web Chat box Plug-ins and Add-ons Javascript 0
H add videoplayer in php5/Mysql PHP 11
Blogmasterpg Add Money Blog Presenta il tuo Sito 0
S XXXLinkReview.com Launches ...Add your links! Presenta il tuo Sito 0
S add new Classic ASP 1
MarcoGrazia foreign key cancellazione degli alberi figli di un nodo mancante MySQL 0
MrSab [MySQL] Foreign key errore MySQL 5
G Can't create table error - foreign key MySQL 0
T foreign key MySQL 2
M [Mysql] problema su Foreign Key MySQL 0

Discussioni simili