MRW.it Forum
  • Home
  • Forum
  • Hosting, Server e Sistemi
  • Database
  • MySQL

Cannot add foreign key constraint

  • Creatore Discussione Creatore Discussione JackIlPazzo
  • Data di inizio Data di inizio 30 Dic 2015
JackIlPazzo

JackIlPazzo

Utente Attivo
25 Lug 2014
69
0
6
  • 30 Dic 2015
  • #1
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

Sergio Unia

Utente Attivo
7 Gen 2016
30
1
8
Todi
www.sunelweb.net
  • 8 Gen 2016
  • #2
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.
 
Devi accedere o registrarti per poter rispondere.

Discussioni simili

L
Error Code: 1215. Cannot add foreign key constraint
  • lisagiacob
  • 29 Mag 2021
  • MySQL
Risposte
3
Visite
1K
MySQL 30 Mag 2021
illiterate2020
I
M
  • Bloccata
non static method cannot be referenced from a static context
  • m.pittini
  • 8 Feb 2021
  • Java
Risposte
1
Visite
1K
Java 8 Feb 2021
Max 1
M
Errore pagina php 'Cannot detect primary key'
  • Max61
  • 6 Gen 2019
  • PHP
Risposte
8
Visite
2K
PHP 8 Gen 2019
Max61
M
G
Errore Cannot read property 'childNodes' of undefined per mancanza nodo nel file xml
  • gapet
  • 2 Ott 2018
  • XML
Risposte
6
Visite
3K
XML 8 Ott 2018
macus_adi
[WordPress] .../ffmpeg/ffmpeg32.so: cannot execute binary file"
  • romeocharly
  • 22 Dic 2017
  • WordPress
Risposte
11
Visite
2K
WordPress 30 Dic 2017
linoma
L
G
[PHP] Warning: Cannot modify header information - headers already sent by
  • giumazzi
  • 23 Feb 2017
  • PHP
Risposte
3
Visite
10K
PHP 24 Feb 2017
claudiolor
C
A
Errore: cannot call methods on dialog prior to initialization; attempted to call method 'open'
  • ans66
  • 18 Dic 2016
  • jQuery
Risposte
4
Visite
3K
jQuery 19 Dic 2016
ans66
A
J
Errore the value cannot be null or empty
  • Jakub Lemiszewski
  • 19 Giu 2015
  • ASP.NET
Risposte
1
Visite
3K
ASP.NET 23 Giu 2015
Vins
V
Cannot send session cache limiter - ERRORE utilizzando session_start();
  • Nik
  • 3 Apr 2015
  • PHP
Risposte
3
Visite
2K
PHP 13 Apr 2015
filomeni
L
Error: int cannot be dereferenced
  • LukeFerrari
  • 10 Gen 2015
  • Java
Risposte
1
Visite
2K
Java 14 Gen 2015
Ansharja
A
Fatal error: Cannot redeclare (function)
  • filippino
  • 10 Ago 2014
  • PHP
Risposte
2
Visite
2K
PHP 10 Ago 2014
maxbossi
L
Cannot find executable for CFBundle
  • LukeFerrari
  • 7 Ago 2014
  • Sviluppo app per iOS
Risposte
0
Visite
2K
Sviluppo app per iOS 7 Ago 2014
LukeFerrari
L
L
ITSM-9000 the bundle identifier cannot be changed from the current value
  • LukeFerrari
  • 9 Apr 2014
  • Sviluppo app per iOS
Risposte
0
Visite
1K
Sviluppo app per iOS 9 Apr 2014
LukeFerrari
L
D
Cannot find XELDZ in all drives
  • davide.samba
  • 21 Mar 2014
  • Windows e Software
Risposte
0
Visite
3K
Windows e Software 21 Mar 2014
davide.samba
D
B
Uncaught TypeError: Cannot read property
  • bitox
  • 11 Dic 2012
  • Javascript
Risposte
7
Visite
5K
Javascript 12 Dic 2012
f107
H
Warning: Cannot modify header information...
  • hackersatifal
  • 18 Set 2012
  • PHP
Risposte
6
Visite
1K
PHP 19 Set 2012
borgo italia
T
Fatal error: Cannot access empty property
  • turillo
  • 26 Lug 2012
  • PHP
Risposte
2
Visite
2K
PHP 26 Lug 2012
turillo
T
A
errore : "550 cannot stor no permission" per pubblicare il sito con expression web
  • alexvicenza
  • 24 Feb 2011
  • Webdesign e Grafica
Risposte
0
Visite
2K
Webdesign e Grafica 24 Feb 2011
alexvicenza
A
A
errore : "550 cannot stor no permission" per pubblicare il sito con expression web
  • alexvicenza
  • 24 Feb 2011
  • HTML e CSS
Risposte
1
Visite
7K
HTML e CSS 27 Feb 2011
SirVester
S
L
Warning: Cannot modify header information - headers already sent by...
  • lucavalentino
  • 27 Apr 2010
  • PHP
Risposte
9
Visite
3K
PHP 27 Mag 2010
borgo italia
Condividi:
Facebook X (Twitter) LinkedIn WhatsApp e-mail Condividi Link
  • Home
  • Forum
  • Hosting, Server e Sistemi
  • Database
  • MySQL
  • Italiano
  • Termini e condizioni d'uso del sito
  • Policy Privacy
  • Aiuto
  • Home
Community platform by XenForo® © 2010-2024 XenForo Ltd. | Traduzione a cura di XenForo Italia
Menu
Accedi

Registrati

  • Home
  • Forum
    • Nuovi Messaggi
    • Cerca...
  • Novità
    • Featured content
    • Nuovi Messaggi
    • Ultime Attività
X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?

X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?