Errno 121, duplicate key on write or update?

后端 未结 4 762
既然无缘
既然无缘 2020-12-07 00:36
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_MO         


        
4条回答
  •  长情又很酷
    2020-12-07 01:02

    This is likely because you have named at least one constraint with the same identifier as a column:

    /* 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)
    

    Should use a different identifier for the constraint like fk_restaurant as in :

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

    And same thing in the food table:

      /* 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)
    

    Those are the only three I see, but there could be others I missed.

提交回复
热议问题