2016-04-27 63 views
0

我得到以下錯誤:ERROR 1215(HY000):無法添加外鍵約束

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.idprimary key unique not null

精品表的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`); 

我不明白。是什麼原因?如何解決這個問題呢?

+0

使用清晰的句子是非常有用的,它不是你的學校練習冊。 – peterh

回答

0

感謝Philipp Dietl提供的解決方案。

在非空列上刪除時,無法添加帶有set null的外鍵約束。

我剛剛修改了boutique_id列到BIGINT(但沒有BIGINT NOT NULL)。

0

請問您的表格client_generique是否存儲boutique_id中的值boutique不存在? 由於您嘗試建立對boutique的引用,因此該值必須存在於該表中。

+0

我剛剛添加了'boutique_id'列,因此它是空的: 'ALTER TABLE client_generique ADD COLUMN標籤VARCHAR(255), ADD COLUMN祕密VARCHAR(32), ADD COLUMN SITE_ID VARCHAR(32), ADD COLUMN boutique_id BIGINT不爲NULL, DROP COLUMN site_marchand_id;' – David

+0

也許數據類型是問題。你使用bigint(20)作爲精品店中的id,並在你的alter-statement中使用bigint作爲表client_generique –

+0

我試過這次沒有成功:'ALTER TABLE client_generique DROP boutique_id; ALTER TABLE client_generique ADD COLUMN boutique_id BIGINT(20)NOT NULL; ALTER TABLE client_generique ADD CONSTRAINT client_generique_boutique_id_boutique_id FOREIGN KEY(boutique_id)REFERENCES boutique(id) ON DELETE SET NULL;'所以數據類型可能不成問題。 – David

相關問題