1
我正在開發一個補丁版本,這是通過創建或更新使用原始sql.When我跑這3個查詢,如現有表的外鍵MySQL的下面無法設置
第一次查詢成功
CREATE TABLE ts_overtime_scheme_histories(id int AUTO_INCREMENT NOT NULL,
name VARCHAR(255),
workdays INT,
break_payable VARCHAR(25),
roundable VARCHAR(25),
round_rule VARCHAR(10), round_minute INT,
type enum('ratio','fixed') DEFAULT 'ratio',
overtime_request_id INT UNSIGNED NOT NULL, PRIMARY KEY(id),
FOREIGN KEY(overtime_request_id) REFERENCES ts_overtime_requests(id));
第二個查詢成功
CREATE TABLE ts_overtime_scheme_details_histories(id INT AUTO_INCREMENT NOT NULL,
hour FLOAT,
ratio FLOAT,
overtime_scheme_history_id INT(11) UNSIGNED NOT NULL,
PRIMARY KEY(id));
現在我試圖將第二個表連接到第一個表。所以第一個表在第二個表上有外鍵。所以我跑了第三個查詢
ALTER TABLE ts_overtime_scheme_details_histories ADD FOREIGN KEY (`overtime_scheme_history_id`) REFERENCES `ts_overtime_scheme_histories` (`id`) ON DELETE CASCADE;
但不知何故失敗。錯誤報告如下
General error: 1005 Can't create table `db_test`.`#sql-ea4_28` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: ALTER TABLE ts_overtime_scheme_details_histories
ADD FOREIGN KEY (`overtime_scheme_history_id`) REFERENCES `ts_overtime_scheme_histories` (`id`)
ON DELETE CASCADE;)
有人能幫我找到我想念的東西嗎?起初,我懷疑主鍵和外鍵的數據長度並不相似,但是當我再次檢查時,它是正確的。
編輯: 表ts_overtime_requests是使用laravel框架創建的。