Errno 121,写入或更新时重复键?

时间:2012-10-21 04:12:28

标签: mysql mysql-workbench workbench

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`restaurants`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`restaurants` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(128) NOT NULL ,
  `description` VARCHAR(1024) NOT NULL ,
  `address` VARCHAR(1024) NOT NULL ,
  `phone` VARCHAR(16) NOT NULL ,
  `url` VARCHAR(128) NOT NULL ,
  `min_order` INT NOT NULL ,
  `food_types` SET('pizza', 'sushi', 'osetian_pie') NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`regions`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`regions` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `restaurant` INT NOT NULL ,
  `name` VARCHAR(128) NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `restaurant_idx` (`restaurant` ASC) ,
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
  CONSTRAINT `restaurant`
    FOREIGN KEY (`restaurant` )
    REFERENCES `mydb`.`restaurants` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`food`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`food` (
  `id` INT NOT NULL ,
  `type` ENUM('pizza', 'sushi', 'osetian_pie') NOT NULL ,
  `name` VARCHAR(45) NOT NULL ,
  `ingredients` VARCHAR(256) NULL ,
  `image` VARCHAR(256) NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`food_variant`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`food_variant` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `size` VARCHAR(16) NOT NULL ,
  `weight` VARCHAR(16) NOT NULL ,
  `price` INT NOT NULL ,
  `food` INT NOT NULL ,
  `restaurant` INT NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
  INDEX `food_idx` (`food` ASC) ,
  INDEX `restaurant_idx` (`restaurant` ASC) ,
  CONSTRAINT `food`
    FOREIGN KEY (`food` )
    REFERENCES `mydb`.`food` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `restaurant`
    FOREIGN KEY (`restaurant` )
    REFERENCES `mydb`.`restaurants` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

Error is:
    Executing SQL script in server
    ERROR: Error 1005: Can't create table 'mydb.food_variant' (errno: 121)

我看不到重复的约束。它在哪里?

4 个答案:

答案 0 :(得分:27)

这可能是因为您已命名至少一个具有与列相同的标识符的约束:

/* You already have a column named `restaurant` in this table, 
   but are naming the FK CONSTRAINT `restaurant` also... */
CONSTRAINT `restaurant`
    FOREIGN KEY (`restaurant` )
    REFERENCES `mydb`.`restaurants` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

应为fk_restaurant之类的约束使用不同的标识符,如:

CONSTRAINT `fk_restaurant`
    FOREIGN KEY (`restaurant` )
    REFERENCES `mydb`.`restaurants` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

同样的事情在food表中:

  /* Name it fk_food */
  CONSTRAINT `fk_food`
    FOREIGN KEY (`food` )
    REFERENCES `mydb`.`food` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  /* Name it fk_restaurant */
  CONSTRAINT `fk_restaurant`
    FOREIGN KEY (`restaurant` )
    REFERENCES `mydb`.`restaurants` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

这是我看到的唯一三个,但可能还有其他我错过了。

答案 1 :(得分:2)

此答案来自@Michael Berkowski的the comment to the answer。我将其发布为答案,因为它实际上对我有用:

  

即使更改约束名称,我也得到了errno 121   跨多个表。问题是,即使跨越不同   表不能具有相同的约束名称。我在用   fk_entryidtable1中的table2,必须将它们更改为   分别使用fk_table1_entryidfk_table2_entryid   工作。

答案 2 :(得分:1)

上面的所有答案都很棒,但是即使我删除了所有表后也无法解决我的问题,但是在我删除数据库并再次创建它之后,一切运行正常,并且迁移运行顺利。...似乎是关键删除表后将对其进行缓存,并且不会将其清除。

注意:这不是问题的答案,但我的经验可能会对他人有所帮助。

答案 3 :(得分:0)

这是对@ peter-moses的回应,因为我遇到的问题和他完全一样。

我通过在转储命令中添加--add-drop-database解决了该问题。其记录在这里:https://mariadb.com/kb/en/mysqldump/

我的最终命令如下:

mysqldump -uroot -ppassword --single-transaction --all-databases --add-drop-database