2017-06-05 16 views
0

私はかなりの記事を見てきましたが、私の問題の解決策は見つかりませんでした。私の疑念は、エラーは私が2つの異なるテーブルで同じプライマリキーの列を参照するために単一の列を使用しようとしているからです。具体的には、bidテーブルには、bidderおよびitem_round_statusテーブルにも存在する外部キーsimulation_idがあります。入札テーブルはこれらのテーブルの両方の外部キーを参照していますが、テーブル内に1つのsimulation_idカラムのみを使用したいと考えています。これはエラー150の問題の原因ですか?エラーメッセージを表示するように更新mysqlエラー150が2つのテーブルの同じ外部キー列を参照しています

-- ----------------------------------------------------- 
-- 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; 

:また、UMLダイアグラムで更新

------------------------ 
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 diagram of three relevant tables

+0

正確なエラーメッセージのテキストは何ですか?また、[外部キーの使用とエラーの情報](https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html)PS「何を使用したいのですか?表中の1つの「シミュレーションID」列は「平均? – philipxy

+0

@philipxy「InnoDBは、外部キーで任意のインデックス列または列のグループを参照できますが、参照されるテーブルでは、参照される列が最初の列としてリストされるインデックスが必要です列は同じ順序で表示されます。入札テーブルの列がsimulation_id、round、item_id、bidder_idxの順番で並べられているという問題はありますか?間の列が問題を引き起こしますか? – german129

答えて

1

Foreign Key Usage and Error Informationは、FK(外部キー)に関する情報を提供します。

最新のInnoDB外部キーエラーの詳細については、SHOW ENGINE INNODB STATUSの出力を確認してください。

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, 

、:暗黙的にデフォルトユニークNOT NULLインデックスを宣言

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

が、他のすべてのインデックスはdoesnのようにFKの列リストから始めてください。

0

Philipxyは、この問題のあなたの助けをありがとうございました。コメントでは、入札者の外部キーが間違っていたとします。なんらかの理由で、mysqlのworkbenchがコードの列を間違った順序で生成しました。

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, 

、次のコードは正常に動作します:mysqlworkbenchによって提供されたコードは以下の通りです

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, 

私の知る限り、私は適切にインデックスのセットアップを持っていた伝えることができますが、コードが生成されたとして、間違った順序で。

(私はSQLスクリプトを生成するためにmysql workbenchフォワードエンジニアを使用しましたが、私はguiを使って入札テーブル、入札者、item_round_statusの間​​の関係を作成していました。私は手動で外部キーに関連するインデックスを変更したitem_round_statusのために生成されたシミュレーションIDの列を参照するように入札者を調整しました手動で適切なインデックスに変更しましたがエラーを引き起こすスクリプトを生成するときに変更を無視するようです。

関連する問題