mysql - 错误 1215 (HY000) 在第 13 行 : Cannot add foreign key constraint

标签 mysql foreign-keys

我收到以下错误:

ERROR 1215 (HY000) at line 13: Cannot add foreign key constraint

对于以下查询:

ALTER TABLE client_generique
ADD CONSTRAINT client_generique_boutique_id_boutique_id
FOREIGN KEY (boutique_id) REFERENCES boutique (id)
  ON DELETE SET NULL;

boutique.id 是一个主键,唯一且不为空

精品表的phpmyadmin结构导出是这样的:

--
-- Table structure for table `boutique`
--

CREATE TABLE `boutique` (
  `id` bigint(20) NOT NULL,
  `nom` varchar(255) NOT NULL,
  `identifiant_site` varchar(8) NOT NULL COMMENT 'the eight number identifier from the bank',
  `certificate` varchar(255) NOT NULL COMMENT 'changes according to the mode, this will be used as salt in the sha1 that will be sent to the bank as the ''signature''',
  `mode` varchar(10) NOT NULL COMMENT 'is the ''vads_ctx_mode'': TEST or PRODUCTION',
  `payment_system` varchar(10) NOT NULL COMMENT 'CYBERPLUS OR PAYZEN for the new payment system added in end 2014'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for table `boutique`
--
ALTER TABLE `boutique`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_mode_per_identifiant_idx` (`identifiant_site`,`mode`);

我不明白。是什么原因?如何解决这个问题?

最佳答案

是否您的表 client_generiqueboutique_id 中存储了表 boutique 中不存在的值? 由于您尝试建立对 boutique 的引用,因此值必须存在于该表中。

关于mysql - 错误 1215 (HY000) 在第 13 行 : Cannot add foreign key constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886580/

相关文章:

php - CSV 导出和下载导致内部服务器错误

mysql - MySQL 中的空间交集

mysql - 使用 mysql 查找日期间隔

mysql - MySQL 中函数组合的歧义

playframework - Play Framework -SQLite:启用外键

Mysql表引用更新

python - 在 Django Rest Framework 中下拉外键选择

php - 使用 php 连接到本地计算机中的生产服务器

django - 如何通过 Django 中的 prefetch_related 过滤具有更多条件的反向外键

sql - 关于外键和主键的 Postgres 和索引