0

I receive the following error when I forward engineer my database:

ERROR: Error 1022: Can't write; duplicate key in table 'recipes'

Any suggestions?

I researched the question in stackoverflow and I thought if I changed the name of some my foreign keys from other tables (some of which have the same foreign key names) there would not be an error. The same error code persists. The schema for my database shows only 2 out of the eight tables.

Executing SQL script in server

ERROR: Error 1022: Can't write; duplicate key in table 'recipes'
SQL Code:
        -- -----------------------------------------------------
        -- Table `our_recipes`.`recipes`
        -- -----------------------------------------------------
        CREATE TABLE IF NOT EXISTS `our_recipes`.`recipes` (
          `recipe_id` INT NOT NULL,
          `chef_id` INT NOT NULL,
          `recipe_name` CHAR(25) NULL,
          `description` LONGTEXT NULL,
          `min_prep` VARCHAR(25) NULL,
          `total_time` INT NULL,
          `servings_min` VARCHAR(25) NULL,
          `servings_max` VARCHAR(45) NULL,
          `directions` VARCHAR(45) NULL,
          `website_id1` VARCHAR(25) NOT NULL,
          PRIMARY KEY (`recipe_id`),
          INDEX `chef_id_idx` (`chef_id` ASC),
          INDEX `website_id_idx` (`website_id1` ASC),
          UNIQUE INDEX `recipe_id_UNIQUE` (`recipe_id` ASC),
          CONSTRAINT `chef_id`
            FOREIGN KEY (`chef_id`)
            REFERENCES `our_recipes`.`chef` (`chef_id`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION,
          CONSTRAINT `website_id`
            FOREIGN KEY (`website_id1`)
            REFERENCES `our_recipes`.`website` (`website_id`)
            ON DELETE NO ACTION
            ON UPDATE NO ACTION)
        ENGINE = InnoDB

I expected to have eight tables in database schema. I also expected no errors.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
ebonie
  • 11
  • 1
  • 2
  • 1
    You don't need `UNIQUE INDEX` on `recipe_id`, since `recipe_id` is `PRIMARY`. `PRIMARY` itself will guarantee that each `recipe_id` will be unique. Another possibilities are your constraint names are found on another table. Make sure that constraint `chef_id` and `website_id` are not being used on another table. – Hermanto Apr 18 '19 at 01:13
  • 3
    Possible duplicate of [Error 1022 - Can't write; duplicate key in table](https://stackoverflow.com/questions/18056786/error-1022-cant-write-duplicate-key-in-table) – sticky bit Apr 18 '19 at 01:16

1 Answers1

0

The error

Error 1022: Can't write; duplicate key in table 'recipes' SQL Code

tells you that you are inserting a record in table recipes with an id or a unique identifier that already exists in the table.

To fix this issue, make sure that there are no duplicate primary keys in your insert statement scripts.

Ian Preglo
  • 421
  • 2
  • 10