i have an issue with my db where i can not insert data into it, it keeps showing me this error : Mysql: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails. I am not trying to create new foreign keys, i am just trying to insert data into tables I have 4 tables, here is the creation code below:
CREATE TABLE IF NOT EXISTS `projectx`.`restaurants` (
`restaurant_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`restaurant_name` VARCHAR(45) NOT NULL COMMENT '',
PRIMARY KEY (`restaurant_id`) COMMENT '',
UNIQUE INDEX `restaurant_id_UNIQUE` (`restaurant_id` ASC) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `projectx`.`table_area`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projectx`.`table_area` (
`area_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`width` INT NOT NULL COMMENT '',
`height` INT NOT NULL COMMENT '',
PRIMARY KEY (`area_id`) COMMENT '',
UNIQUE INDEX `area_id_UNIQUE` (`area_id` ASC) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `projectx`.`table_position`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projectx`.`table_position` (
`position_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`pos_x` INT NOT NULL COMMENT '',
`pos_y` INT NOT NULL COMMENT '',
PRIMARY KEY (`position_id`) COMMENT '',
UNIQUE INDEX `position_id_UNIQUE` (`position_id` ASC) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `projectx`.`restaurant_tables`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projectx`.`restaurant_tables` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`table_id` INT NOT NULL COMMENT '',
`restaurant_id` INT NOT NULL COMMENT '',
`table_kind` VARCHAR(10) NULL COMMENT '',
`number_of_seats` INT NOT NULL COMMENT '',
`is_taken` TINYINT(1) NOT NULL COMMENT '',
`table_area_id` INT NOT NULL COMMENT '',
`table_position_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`id`, `restaurant_id`, `table_area_id`, `table_position_id`) COMMENT '',
UNIQUE INDEX `id_UNIQUE` (`id` ASC) COMMENT '',
INDEX `fk_restaurant_tables_restaurants_idx` (`restaurant_id` ASC) COMMENT '',
INDEX `fk_restaurant_tables_table_area1_idx` (`table_area_id` ASC) COMMENT '',
INDEX `fk_restaurant_tables_table_position1_idx` (`table_position_id` ASC) COMMENT '',
CONSTRAINT `fk_restaurant_tables_restaurants`
FOREIGN KEY (`restaurant_id`)
REFERENCES `projectx`.`restaurants` (`restaurant_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_restaurant_tables_table_area1`
FOREIGN KEY (`table_area_id`)
REFERENCES `projectx`.`table_area` (`area_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_restaurant_tables_table_position1`
FOREIGN KEY (`table_position_id`)
REFERENCES `projectx`.`table_position` (`position_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Insert code :
insert into restaurant_tables(id,table_id,restaurant_id,table_kind,number_of_seats,is_taken,table_area_id,table_position_id) values
(1,1,1,'square',4,0,1,1);
Basically the idea is for 1 restaurant to be able to have more tables, and each table must have only one width,height and pos_x, pos_y. What am i doing wrong here?Is the table layout and constraints good? Any help would be awesome! Thanks!