2017-06-05 97 views
0

我已經看了好幾篇文章,但沒有找到我的問題的解決方案。我的懷疑是錯誤源於我試圖用一個列引用兩個不同表中的同一主鍵列。具體來說,該出價表具有也存在於投標人和item_round_status表中的外鍵simulation_id。出價表引用了這兩個表的外鍵,但我希望在表中只使用一個simulation_id列。這是錯誤150問題的來源嗎?從兩個表中引用相同的外鍵列的MySQL錯誤150

-- ----------------------------------------------------- 
-- Table `kffg_simulations`.`item_round_status` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`item_round_status` (
    `simulation_id` INT NOT NULL , 
    `round` INT NOT NULL , 
    `clock_item_id` INT NOT NULL , 
    `posted_price` BIGINT NOT NULL , 
    `clock_price` BIGINT NOT NULL , 
    PRIMARY KEY (`simulation_id`, `round`, `clock_item_id`) , 
    INDEX `fk_item_round_status_clock_item1_idx` (`clock_item_id` ASC) , 
    INDEX `fk_item_round_status_simulation1_idx` (`simulation_id` ASC) , 
    CONSTRAINT `fk_item_round_status_clock_item1` 
    FOREIGN KEY (`clock_item_id`) 
    REFERENCES `kffg_simulations`.`clock_item` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_item_round_status_simulation1` 
    FOREIGN KEY (`simulation_id`) 
    REFERENCES `kffg_simulations`.`simulation` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `kffg_simulations`.`bidder` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bidder` (
    `simulation_id` INT NOT NULL , 
    `idx` INT NOT NULL , 
    `bidder_strategy_id` INT NOT NULL , 
    `budget` BIGINT NOT NULL , 
    PRIMARY KEY (`simulation_id`, `idx`) , 
    INDEX `fk_bidder_simulation1_idx` (`simulation_id` ASC) , 
    INDEX `fk_bidder_bidder_strategy1_idx` (`bidder_strategy_id` ASC) , 
    CONSTRAINT `fk_bidder_simulation1` 
    FOREIGN KEY (`simulation_id`) 
    REFERENCES `kffg_simulations`.`simulation` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bidder_bidder_strategy1` 
    FOREIGN KEY (`bidder_strategy_id`) 
    REFERENCES `kffg_simulations`.`bidder_strategy` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `kffg_simulations`.`bid_type` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid_type` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `name` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`id`) ) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `kffg_simulations`.`bid_status` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid_status` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `description` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`id`) ) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `kffg_simulations`.`bid` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `kffg_simulations`.`bid` (
    `simulation_id` INT NOT NULL , 
    `item_round_status_round` INT NOT NULL , 
    `clock_item_id` INT NOT NULL , 
    `bidder_idx` INT NOT NULL , 
    `quantity` INT NOT NULL , 
    `price` INT NOT NULL , 
    `bid_type_id` INT NOT NULL , 
    `switch_to_pea_category_id` INT NOT NULL , 
    `backstop` BIGINT NULL , 
    `bid_status_id` INT NOT NULL , 
    `processed_demand` INT NOT NULL , 
    PRIMARY KEY (`simulation_id`, `item_round_status_round`, `clock_item_id`, `bidder_idx`, `quantity`) , 
    INDEX `fk_bid_item_round_status1_idx` (`simulation_id` ASC, `item_round_status_round` ASC, `clock_item_id` ASC) , 
    INDEX `fk_bid_bidder1_idx` (`simulation_id` ASC, `bidder_idx` ASC) , 
    INDEX `fk_bid_bid_type1_idx` (`bid_type_id` ASC) , 
    INDEX `fk_bid_pea_category1_idx` (`switch_to_pea_category_id` ASC) , 
    INDEX `fk_bid_bid_status1_idx` (`bid_status_id` ASC) , 
    CONSTRAINT `fk_bid_item_round_status1` 
    FOREIGN KEY (`simulation_id` , `item_round_status_round` , `clock_item_id`) 
    REFERENCES `kffg_simulations`.`item_round_status` (`simulation_id` , `round` , `clock_item_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bidder1` 
    FOREIGN KEY (`bidder_idx` , `simulation_id`) 
    REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bid_type1` 
    FOREIGN KEY (`bid_type_id`) 
    REFERENCES `kffg_simulations`.`bid_type` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_pea_category1` 
    FOREIGN KEY (`switch_to_pea_category_id`) 
    REFERENCES `kffg_simulations`.`pea_category` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bid_status1` 
    FOREIGN KEY (`bid_status_id`) 
    REFERENCES `kffg_simulations`.`bid_status` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 

更新,顯示錯誤消息:

------------------------ 
LATEST FOREIGN KEY ERROR 
------------------------ 
170604 21:52:27 Error in foreign key constraint of table kffg_simulations/bid: 

    FOREIGN KEY (`simulation_id` , `item_round_status_round` , `clock_item_id`) 
    REFERENCES `kffg_simulations`.`item_round_status` (`simulation_id` , `round` , `clock_item_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bidder1` 
    FOREIGN KEY (`bidder_idx` , `simulation_id`) 
    REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bid_type1` 
    FOREIGN KEY (`bid_type_id`) 
    REFERENCES `kffg_simulations`.`bid_type` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_pea_category1` 
    FOREIGN KEY (`switch_to_pea_category_id`) 
    REFERENCES `kffg_simulations`.`pea_category` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_bid_bid_status1` 
    FOREIGN KEY (`bid_status_id`) 
    REFERENCES `kffg_simulations`.`bid_status` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB: 
Cannot find an index in the referenced table where the 
referenced columns appear as the first columns, or column types 
in the table and the referenced table do not match for constraint. 
Note that the internal storage type of ENUM and SET changed in 
tables created with >= InnoDB-4.1.12, and such columns in old tables 
cannot be referenced by such columns in new tables. 
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html 
for correct foreign key definition. 

與UML圖也更新: UML diagram of three relevant tables

+0

什麼是確切的錯誤信息文本?另外:[外鍵使用和錯誤信息](https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html)PS什麼「但我只想使用表中的一個simulation_id列「是什麼意思? – philipxy

+0

@philipxy我唯一能看到的就是這個語句:「InnoDB允許一個外鍵引用任何索引列或者一組列,但是在被引用的表中,必須有一個索引,其中被引用的列被列爲第一個列順序相同「。投標人表鍵列是否爲simulation_id,bidder_idx時,是否存在出價表列的順序爲simulation_id,round,item_id,bidder_idx的問題?是否在兩個列之間導致問題? – german129

回答

1

Foreign Key Usage and Error Information給出有關FK(外鍵)的信息。

您可以通過檢查SHOW ENGINE INNODB STATUS的輸出來獲取最新InnoDB外鍵錯誤的詳細說明。

InnoDB的允許外鍵引用列的任何索引列或組。但是,在被引用的表中,必須有一個索引,其中被引用的列按照相同順序列爲第一列。

在競價:

FOREIGN KEY (`bidder_idx` , `simulation_id`) 
REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`) 

的 「參考表」 這裏是競價,在 「引用的列」 列表(IDX,simulation_id)。

Cannot find an index in the referenced table where the 
referenced columns appear as the first columns, 

果然,在投標人發現的最接近的是:

PRIMARY KEY (`simulation_id`, `idx`) , 

其中隱含聲明瞭一個默認的唯一不爲空指數,但像所有其他指標沒有按從FK的專欄列表開始。

0

Philipxy感謝你對這個問題的幫助。您在評論中認爲投標人的外鍵錯誤是正確的。出於某種原因,mysql工作臺以錯誤的順序在代碼中生成了列。通過mysqlworkbench提供的代碼如下:

CONSTRAINT `fk_bid_bidder1` 
    FOREIGN KEY (`bidder_idx` , `simulation_id`) 
    REFERENCES `kffg_simulations`.`bidder` (`idx` , `simulation_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 

和下面的代碼工作正常:

CONSTRAINT `fk_assignment_bidder1` 
    FOREIGN KEY (`bidder_simulation_id` , `bidder_idx`) 
    REFERENCES `kffg_simulations`.`bidder` (`simulation_id` , `idx`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 

至於我可以告訴我正確了索引設置,但該代碼被生成順序錯誤。我使用了gui來創建出價表,出價者和item_round_status之間的關係,因爲這兩個表的模擬都是pk,所以它重複了那個列。當我使用mysql工作臺前端工程師生成sql腳本時。我手動調整了投標人的外鍵,以引用爲item_round_status生成的simulationid列,它改變了與該外鍵相關的索引。我手動將它們更改爲正確的索引,但它在生成導致錯誤的腳本時似乎忽略了我的更改。