Bonjour,

J'ai les définitions de tables suivantes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
CREATE TABLE IF NOT EXISTS `dat_users` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_subscriber` int(10) UNSIGNED NOT NULL,
  `login` varchar(12) NOT NULL,
  `hash` varchar(255) DEFAULT NULL,
  `user_type` tinyint(1) NOT NULL DEFAULT '1',
  `rights` varchar(50) NOT NULL DEFAULT '0-2,1-2,2-2,3-2,4-2,5-2,6-2,7-2,8-2,9-2',
  `token` varchar(255) DEFAULT NULL,
  `checked` tinyint(1) DEFAULT NULL,
  `banned` tinyint(1) DEFAULT NULL,
  `is_writer` tinyint(1) UNSIGNED DEFAULT NULL,
  `create_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `code` (`id_subscriber`,`login`),
  KEY `user_type` (`user_type`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
 
ALTER TABLE `dat_users`
  ADD CONSTRAINT `dat_users_ibfk_1` FOREIGN KEY (`id_subscriber`) REFERENCES `dat_subscribers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
 
CREATE TABLE IF NOT EXISTS `lst_salesagents` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_subscriber` int(10) UNSIGNED NOT NULL,
  `agentname` varchar(20) DEFAULT NULL,
  `user_type` int(1) UNSIGNED DEFAULT NULL,
  `rank` int(10) UNSIGNED DEFAULT NULL,
  `create_date` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `id_subscriber` (`id_subscriber`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
Lorsque j'essaie de créer une contrainte de clef étrangère sur la deuxième table comme ceci ALTER TABLE `lst_salesagents` ADD FOREIGN KEY (`user_type`) REFERENCES `dat_users`(`user_type`) ON DELETE SET NULL ON UPDATE CASCADE;, j'obtiens une erreur
#1215 - Impossible d'ajouter des contraintes d'index externe
J'ai regardé la doc et je ne vois pas ce qui ne va pas.