2015-07-21 153 views
0

我試着在我的數據庫中創建一個表「SIGNATURE」我想要這個表有一個雙主鍵,並將外鍵約束添加到兩個主鍵引用表「TRAITEMENT」MySql 5.6.17錯誤#1215 - 無法添加外鍵約束

CREATE TABLE TRAITEMENT (
id INTEGER NOT NULL, 
dateHeurePrevue DATETIME NOT NULL, 
commentaire LONGTEXT, 
dateValidation TIMESTAMP, 
CONSTRAINT PK PRIMARY KEY (id,dateHeurePrevue), 
CONSTRAINT FK_traitement_consigne FOREIGN KEY (id) REFERENCES consigne(id))  ENGINE=InnoDB; 
create unique index id_dateheureprevue on traitement(id, dateheureprevue); 

CREATE TABLE SIGNATURE (
id INTEGER NOT NULL, 
idPersonne INTEGER NOT NULL, 
dateTimeTraitement DATETIME NOT NULL, 
retard INTEGER, 
motifRetard INTEGER, 
heureEffectiveTraitement DATETIME NOT NULL, 
CONSTRAINT PK PRIMARY KEY (id,dateTimeTraitement), 
CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES traitement(id), 
CONSTRAINT FK_idPersonne FOREIGN KEY (idPersonne) REFERENCES personne(id), 
CONSTRAINT FK_motifRetard FOREIGN KEY (motifRetard) REFERENCES retard(id), 
CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (dateTimeTraitement) REFERENCES traitement(dateHeurePrevue))ENGINE=InnoDB; 
create unique index id_dateTimetraitement on signature(id, dateTimeTraitement); 

顯然,(我想刪除它,它工作得很好)的問題來源於此forign鍵:

CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (dateTimeTraitement) REFERENCES traitement(dateHeurePrevue) 

我不明白,我不看到任何類型的衝突,鍵... 如果有人能幫我解決這個問題LEM ...

在此先感謝

+0

難道是因爲主要的你外鍵語句只引用一部分表的主鍵? – Osuwariboy

+0

檢查您的整理類型 – wahmal

回答

0

你的外鍵必須正好等於主鍵字段,所以:

CONSTRAINT FK_dateTimeTraitement FOREIGN KEY (id, dateTimeTraitement) REFERENCES traitement(id, dateHeurePrevue) 
相關問題