2017-08-29 59 views
0

我嘗試添加外鍵:無法添加外鍵的MySQL:錯誤號150

CREATE TABLE `invoices` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    ` name` varchar(255) NOT NULL, 
    `code_text` text NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 




    CREATE TABLE `leads` (
    `lead_id` int(11) NOT NULL AUTO_INCREMENT, 
    ... 
    PRIMARY KEY (`lead_id`) 
); 



CREATE TABLE `leads_invoices` (
leads_invoice_id int(11) AUTO_INCREMENT NOT NULL, 
    invoice_id int(11) NOT NULL, 
    lead_id_i int(11) NOT NULL, 
    PRIMARY KEY(leads_invoice_id), 
    FOREIGN KEY (invoice_id) REFERENCES invoices(id), 
    FOREIGN KEY (lead_id_i) REFERENCES leads(lead_id) 
    ON UPDATE CASCADE 
    ON DELETE RESTRICT 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

但我得到的錯誤與我lead_id_i鍵:

無法創建表crm_testleads_invoices(錯誤:150「外國 鍵約束的格式不正確」)

我檢查了所有類型和表,看來他們是正確的.. 我怎樣才能解決這個問題?希望你能幫助我。謝謝!

回答

4

您必須添加ENGINE=InnoDB DEFAULT CHARSET=utf8到引線表

CREATE TABLE `leads` (
    `lead_id` int(11) NOT NULL AUTO_INCREMENT, 
    ... 
    PRIMARY KEY (`lead_id`) 
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 

因爲如果你想添加一個外鍵,兩個表必須具有相同的引擎

+0

謝謝,它救了我 –

+0

@ babboe2babboe2歡迎您 – Jens