I'm having trouble with forwarding database to server. Here's log:
Executing SQL script in server
ERROR: Error 3780: Referencing column 'id_users' and referenced column 'id_users' in foreign key constraint 'fk_watch_later_users1' are incompatible.
SQL Code:
Table mydb.watch_later
CREATE TABLE IF NOT EXISTS `mydb`.`watch_later` (
`id_wl` INT NOT NULL,
`id_users` INT NULL,
`id_films` INT NULL,
PRIMARY KEY (`id_wl`),
UNIQUE INDEX `id_wl_UNIQUE` (`id_wl` ASC) VISIBLE,
INDEX `fk_watch_later_films1_idx` (`id_films` ASC) VISIBLE,
INDEX `fk_watch_later_users1_idx` (`id_users` ASC) VISIBLE,
CONSTRAINT `fk_watch_later_films1`
FOREIGN KEY (`id_films`)
REFERENCES `mydb`.`films` (`id_films`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_watch_later_users1`
FOREIGN KEY (`id_users`)
REFERENCES `mydb`.`users` (`id_users`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 15 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Table mydb.users:
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`id_users` INT NOT NULL,
`users_nickname` VARCHAR(45) NULL,
`users_name` VARCHAR(45) NULL,
`users_surname` VARCHAR(45) NULL,
`users_birth` DATE NULL,
PRIMARY KEY (`id_users`),
UNIQUE INDEX `id_users_UNIQUE` (`id_users` ASC) VISIBLE
) ENGINE = InnoDB;
Tell me please how can I fix it? MySQL version is 8.0.18
Related
I've made this ER diagram for my college project but i have to use forward engineering to see SQL code. MySQL version is 8.0.27
when i try it, it come up with error like below.
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'editor_ismi' for constraint 'fk_editor_ismi' in the referenced table 'editor'
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`kitap`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`kitap` (
`isbn` VARCHAR(45) NOT NULL,
`editor_ismi` VARCHAR(45) NULL,
`yazar_ismi` VARCHAR(45) NULL,
`yazar_adres` VARCHAR(45) NULL,
`kitap_yili` INT NOT NULL,
`kitap_baslik` VARCHAR(45) NOT NULL,
`kitap_fiyat` VARCHAR(45) NOT NULL,
PRIMARY KEY (`isbn`),
INDEX `fk_editor_ismi_idx` (`editor_ismi` ASC) VISIBLE,
INDEX `fk_yazar_ismi_idx` (`yazar_ismi` ASC) VISIBLE,
INDEX `fk_yazar_adres_idx` (`yazar_adres` ASC) VISIBLE,
CONSTRAINT `fk_editor_ismi`
FOREIGN KEY (`editor_ismi`)
REFERENCES `mydb`.`editor` (`editor_ismi`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_yazar_ismi`
FOREIGN KEY (`yazar_ismi`)
REFERENCES `mydb`.`Yazar` (`yazar_ismi`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_yazar_adres`
FOREIGN KEY (`yazar_adres`)
REFERENCES `mydb`.`Yazar` (`yazar_ismi`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
screenshot of my er diagram
no rows returned picture, me sql code
insertions
I made the insertions, applied the changes.
here's a script:
-- Table `db`.`TrainIn`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `db`.`TrainIn` (
`TrNumInRec` INT NOT NULL,
`DateIn` VARCHAR(45) NULL,
`TrNum` INT NULL,
`WayInNum` INT NULL,
PRIMARY KEY (`TrNumInRec`),
INDEX `WayInNum_idx` (`WayInNum` ASC),
CONSTRAINT `WayInNum`
FOREIGN KEY (`WayInNum`)
REFERENCES `db`.`Towns` (`TownName`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm trying to make a many-to-many relationship between two tables In Mysql WorkBench, and one of those 2 tables has a composite primary key ( parts are coming from 2 foreign keys). When I'm trying to generate the SQL I'm getting this error :
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `A_D_schema`.`Resources_has_OwnerGroups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `A_D_schema`.`Resources_has_OwnerGroups` (
`Resources_id` INT NOT NULL,
`OwnerGroups_id` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Instances_idInstances` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Customers_idCustomers` INT NOT NULL,
PRIMARY KEY (`Resources_id`, `OwnerGroups_id`, `OwnerGroups_Instances_has_Customers_Instances_idInstances`, `OwnerGroups_Instances_has_Customers_Customers_idCustomers`),
INDEX `fk_Resources_has_OwnerGroups_OwnerGroups1_idx` (`OwnerGroups_id` ASC, `OwnerGroups_Instances_has_Customers_Instances_idInstances` ASC, `OwnerGroups_Instances_has_Customers_Customers_idCustomers` ASC),
INDEX `fk_Resources_has_OwnerGroups_Resources1_idx` (`Resources_id` ASC),
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
From the SHOW ENGINE INNODB STATUS I can see this message :
Cannot resolve column name close to:
)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The SHOW CREATE TABLE Resources and SHOW CREATE TABLE OwnerGroups :
CREATE TABLE `Resources` (
`idResources` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(45) DEFAULT NULL,
`role` int(11) DEFAULT NULL COMMENT 'role : 1 disptcher \n0 admin',
PRIMARY KEY (`idResources`),
UNIQUE KEY `idresources_UNIQUE` (`idResources`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `OwnerGroups` (
`idOwnerGroups` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`group` int(11) DEFAULT NULL,
PRIMARY KEY (`idOwnerGroups`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`id`)
Your Resources table doesn't have a column id. Its primary key is idResources.
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
Your OwnerGroups table doesn't have a column id. Its primary key is idOwnerGroups. It doesn't have the other two columns you reference at all.
In general, when you declare a foreign key, first you name the columns in the child table:
CREATE TABLE Child (
childCol1 INT,
childCol2 INT,
...
FOREIGN KEY (childCol1, childCol2) ...
Then you reference columns in the parent table:
... REFERENCES Parent (parentCol1, parentCol2)
);
You must use the names of columns as they exist in the parent table.
The columns you reference in the parent table must together be the PRIMARY KEY or UNIQUE KEY of that table. In other words, given the example above, it would not work against this Parent table:
CREATE TABLE Parent (
parentCol1 INT,
parentCol2 INT,
PRIMARY KEY (parentCol1)
);
Because the PRIMARY KEY does not include parentCol2.
In your case, the following should work:
CREATE TABLE IF NOT EXISTS `A_D_schema`.`Resources_has_OwnerGroups` (
`Resources_id` INT NOT NULL,
`OwnerGroups_id` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Instances_idInstances` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Customers_idCustomers` INT NOT NULL,
PRIMARY KEY (`Resources_id`, `OwnerGroups_id`, `OwnerGroups_Instances_has_Customers_Instances_idInstances`, `OwnerGroups_Instances_has_Customers_Customers_idCustomers`),
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`idResources`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id`)
REFERENCES `A_D_schema`.`OwnerGroups` (`idOwnerGroups`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB
I took out a couple of INDEX definitions that are redundant. You don't need to index your PRIMARY KEY, it's already the clustered index of the table. You don't need to index the column you use in a foreign key declaration, MySQL will index that column automatically if it need to (though if an index already exists for that column, the FK constraint will use that index).
I'm not sure I understand what your other two columns OwnerGroups_Instances_has_Customers_Instances_idInstances and OwnerGroups_Instances_has_Customers_Customers_idCustomers are meant to do. Typically in a many-to-many table, you only need enough columns to reference the primary keys of the respective parent tables.
Re your comment:
You should try refreshing the view of the schema from time to time. There's a button with a pair of curvy arrows, to the right of "SCHEMAS".
I have two tables on different databases in mysql. I'm trying to create a composite foreign key from one table to the other and for some reason it doesn't work. It only works if I use a single primary key field instead of a composite key.
The problematic constraint in the example below is fk_dummy_table11. I get Error Code: 1215. Cannot add foreign key constraint when executing the statement.
Create table statement for "dummy" in DB NREAP:
CREATE TABLE IF NOT EXISTS `NREAP`.`dummy` (
`id` INT NOT NULL,
`table1_TDO_COD_TIP_DOC` VARCHAR(14) NOT NULL,
`table1_RFI_NUM_DOC` INT NOT NULL,
`table1_RFI_VER_DOC` INT NOT NULL,
`table1_RFI_NOM_FIC` VARCHAR(128) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_dummy_table11_idx` (`table1_TDO_COD_TIP_DOC` ASC, `table1_RFI_NUM_DOC` ASC, `table1_RFI_VER_DOC` ASC, `table1_RFI_NOM_FIC` ASC),
CONSTRAINT `fk_dummy_table11`
FOREIGN KEY (`table1_TDO_COD_TIP_DOC` , `table1_RFI_NUM_DOC` , `table1_RFI_VER_DOC` , `table1_RFI_NOM_FIC`)
REFERENCES `TRANS`.`table1` (`TDO_COD_TIP_DOC` , `RFI_NUM_DOC` , `RFI_VER_DOC` , `RFI_NOM_FIC`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Create table statement for "table1" in DB TRANS:
CREATE TABLE IF NOT EXISTS `TRANS`.`table1` (
`TDO_COD_TIP_DOC` VARCHAR(14) NOT NULL,
`RFI_NUM_DOC` INT NOT NULL,
`RFI_VER_DOC` INT NOT NULL,
`RFI_NOM_FIC` VARCHAR(128) NOT NULL,
`RFI_LOC_FIC` VARCHAR(1000) NULL,
`RFI_DES_FIC` VARCHAR(255) NULL,
`RFI_TIPO` VARCHAR(100) NOT NULL,
`DAT_ALT` DATE NOT NULL,
`COD_UTI_ALT` VARCHAR(14) NOT NULL,
`DFI_VER_DOC` INT NULL,
`DFI_NUM_SEQ` INT NULL,
`CAM_ANO_INI_CAM` VARCHAR(4) NULL,
PRIMARY KEY (`TDO_COD_TIP_DOC`, `RFI_NUM_DOC`, `RFI_VER_DOC`, `RFI_NOM_FIC`))
ENGINE = InnoDB;
Please help, this is so frustrating...
Edit1 - I'm running mysql version 5.7.12
Edit2 - I've ran the SHOW ENGINE INNODB STATUS command, it gives me the following output:
I'm running mysql version 5.7.12, it's the latest one I think.
I've ran innodb status:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-07-13 10:28:34 0x124c Error in foreign key constraint of table nreap/dummy:
FOREIGN KEY (`table1_TDO_COD_TIP_DOC` , `table1_RFI_NUM_DOC` , `table1_RFI_VER_DOC` , `table1_RFI_NOM_FIC`)
REFERENCES `TRANS`.`table1` (`TDO_COD_TIP_DOC` , `RFI_NUM_DOC` , `RFI_VER_DOC` , `RFI_NOM_FIC`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
I've eventually figured out the problem.
I've narrowed it down to a problem in using VARCHAR keys, the restraint worked fine with other datatypes. After a few experiments I've added a specific charset for each schema and it worked. Adding the specific charset to each table also works.
Thanks for the help!
I am trying to forward engineer a database on MySQL Workbench, but I continually am getting this error message. I feel as though it is most likely an issue with the structure of my relationships, but I cannot find a discrepancy.
Here is an image of the schema diagram.
Here is the error message I am receiving:
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `bturpin`.`THREAD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bturpin`.`THREAD` (
`id` INT NOT NULL AUTO_INCREMENT,
`answer` DOUBLE NULL,
`comment` VARCHAR(255) NULL,
`test_subcategory_name` VARCHAR(45) NULL,
`RATING_id` INT NOT NULL,
`RATING_INSTRUCTOR_id` VARCHAR(30) NOT NULL,
`RATING_INSTRUCTOR_LESSON_title` VARCHAR(45) NOT NULL,
`RATING_INSTRUCTOR_LESSON_COURSE_num` VARCHAR(15) NOT NULL,
`RATING_RATER_id` VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`, `RATING_id`, `RATING_INSTRUCTOR_id`, `RATING_INSTRUCTOR_LESSON_title`, `RATING_INSTRUCTOR_LESSON_COURSE_num`, `RATING_RATER_id`),
INDEX `fk_THREAD_RATING1_idx` (`RATING_id` ASC, `RATING_INSTRUCTOR_id` ASC, `RATING_INSTRUCTOR_LESSON_title` ASC, `RATING_INSTRUCTOR_LESSON_COURSE_num` ASC, `RATING_RATER_id` ASC),
CONSTRAINT `fk_THREAD_RATING1`
FOREIGN KEY (`RATING_id` , `RATING_INSTRUCTOR_id` , `RATING_INSTRUCTOR_LESSON_title` , `RATING_INSTRUCTOR_LESSON_COURSE_num` , `RATING_RATER_id`)
REFERENCES `bturpin`.`RATING` (`id` , `INSTRUCTOR_id` , `INSTRUCTOR_LESSON_title` , `INSTRUCTOR_LESSON_COURSE_num` , `RATER_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 14 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Try forwarding in this order :
Manually create the fields which must have a foreign key constraint in MySQL WB
Forward engineer to your DB
Manually create the foreign key constraints in MySQL WB
Forward engineer angain
You may also consider using only integer foreign key.
This seems to be a MySQL Workbench issue with foreign key on non integer key.
Source : Error 1215: Cannot add foreign key constraint