简体   繁体   中英

SQL Error 1215 Cannot add foreign key restraint?

enter image description here i am making a database for a project at school, i live in the netherlands so the naming in this piece of sql will be dutch so please excuse that.

The problem i am having is that i can't seem to create a foreign key and i cannot figure out why.

Here is the piece of sql thats giving me trouble, i am using MySQL.

Executing SQL script in server

ERROR: Error 1215: Cannot add foreign key constraint

SQL Code:


    -- Table `Producten`.`Bestelregel`
    -- -----------------------------------------------------
    CREATE TABLE IF NOT EXISTS `Producten`.`Bestelregel` (
      `Bestelnr` INT NOT NULL COMMENT '',
      `Productnr` INT NOT NULL COMMENT '',
      `Aantal` INT NOT NULL COMMENT '',
      PRIMARY KEY (`Bestelnr`, `Productnr`)  COMMENT '',
      INDEX `Product_idx` (`Productnr` ASC)  COMMENT '',
      CONSTRAINT `Product`
        FOREIGN KEY (`Productnr`)
        REFERENCES `Producten`.`Product` (`nr`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `Bestelling`
        FOREIGN KEY (`Bestelnr`)
        REFERENCES `Producten`.`Bestelling` (`Nr`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB

I have read some other questions on this same problems but i couldn't find any that worked for me, some suggested that my primary key and foreign key had to be of the same type which is true for both constraints.

Also a little side note, this was a forward engineered script from a diagram in mysql workbench.

This is the diagram.

Also set foreign_key_checks=0 is already at the top of my sql script.

set foreign_key_checks=0 write top of your sql file

set foreign_key_checks=1 write bottom of your sql file

I think its help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM