Error 1005 when Forward Engineering EER diagram - mysql

I have created an EER diagram (as shown in the image bellow) in MySQL Workbench and wanted to Forward Engineer to build the DB.
After configuring the model options to the Target MySQL Version and removing the word "Visible" from all the Indexes in the SQL Code (the code goes in the bottom of this post), as it was triggering an error, I have came across a 1005 Error:
Executing SQL script in server
ERROR: Error 1005: Can't create table `books`.`books` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Any guidance on how to solve it is appreciated.
The SQL Code that I am using is the following:
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `books` DEFAULT CHARACTER SET utf8 ;
USE `books` ;
-- -----------------------------------------------------
-- Table `books`.`book_prices`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_prices` (
`book_priceID` INT(11) NOT NULL AUTO_INCREMENT,
`bookID` INT(11) NULL,
`price` DECIMAL(10,2) NULL,
`currency` CHAR(2) NULL,
`date_start` DATETIME NULL,
`date_end` DATETIME NULL,
PRIMARY KEY (`book_priceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transaction_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transaction_types` (
`transaction_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`transactionID` INT(11) NULL,
PRIMARY KEY (`transaction_typeID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transactions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transactions` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_transactions_transaction_types1`
FOREIGN KEY (`transaction_types_transaction_typeID`)
REFERENCES `books`.`transaction_types` (`transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batch_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batch_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`batchID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_batch_transaction_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_batch_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batches`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batches` (
`batchID` INT(11) NOT NULL AUTO_INCREMENT,
`batch_transaction_transactionID` INT(11) NULL,
`book_typeID` INT(11) NOT NULL,
`price` DECIMAL(10,2) NULL,
`supplierID` INT(11) NULL,
PRIMARY KEY (`batchID`),
INDEX `fk_batches_batch_transaction1_idx` (`batch_transaction_transactionID` ASC),
CONSTRAINT `fk_batches_batch_transaction1`
FOREIGN KEY (`batch_transaction_transactionID`)
REFERENCES `books`.`batch_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`suppliers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`suppliers` (
`supplierID` INT(11) NOT NULL AUTO_INCREMENT,
`batches_batchID` INT(11) NOT NULL,
`name` VARCHAR(255) NULL,
PRIMARY KEY (`supplierID`),
INDEX `fk_suppliers_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_suppliers_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customer_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customer_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`customerID` INT(11) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_client_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_customer_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customers` (
`customerID` INT(11) NOT NULL AUTO_INCREMENT,
`books_bookID` INT(11) NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`customer_transaction_transactionID` INT(11) NOT NULL,
PRIMARY KEY (`customerID`),
INDEX `fk_clients_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_clients_client_transaction1_idx` (`customer_transaction_transactionID` ASC),
CONSTRAINT `fk_customrs_products1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customers_customer_transaction1`
FOREIGN KEY (`customer_transaction_transactionID`)
REFERENCES `books`.`customer_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discounts` (
`discountID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`transactions_transactionID` INT(11) NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NULL,
PRIMARY KEY (`discountID`),
INDEX `fk_discounts_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_discounts_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discount_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discount_types` (
`discount_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`Type` VARCHAR(255) NULL,
`discounts_discountID` INT(11) NOT NULL,
PRIMARY KEY (`discount_typeID`),
INDEX `fk_discount_types_discounts1_idx` (`discounts_discountID` ASC),
CONSTRAINT `fk_discount_types_discounts1`
FOREIGN KEY (`discounts_discountID`)
REFERENCES `books`.`discounts` (`discountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`storagedistribution`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`storagedistribution` (
`storagedistributionID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
PRIMARY KEY (`storagedistributionID`),
INDEX `fk_storagedistribution_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_storagedistribution_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
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;
Edit 1:
Removed transaction_typeID from both books batch_transaction tables and ran the following SQL Script:
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema books
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `books` DEFAULT CHARACTER SET utf8 ;
USE `books` ;
-- -----------------------------------------------------
-- Table `books`.`book_prices`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_prices` (
`book_priceID` INT(11) NOT NULL AUTO_INCREMENT,
`bookID` INT(11) NULL,
`price` DECIMAL(10,2) NULL,
`currency` CHAR(2) NULL,
`date_start` DATETIME NULL,
`date_end` DATETIME NULL,
PRIMARY KEY (`book_priceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transaction_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transaction_types` (
`transaction_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`transactionID` INT(11) NULL,
PRIMARY KEY (`transaction_typeID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`transactions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`transactions` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`transaction_types_transaction_typeID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_transactions_transaction_types1`
FOREIGN KEY (`transaction_types_transaction_typeID`)
REFERENCES `books`.`transaction_types` (`transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`books`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`books` (
`bookID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`price` DECIMAL(10,2) NULL,
`book_prices_book_priceID` INT(11) NOT NULL,
`book_types_book_typeID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`ISBN` VARCHAR(13) NULL,
PRIMARY KEY (`bookID`),
INDEX `fk_books_book_prices1_idx` (`book_prices_book_priceID` ASC),
INDEX `fk_books_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_books_book_prices1`
FOREIGN KEY (`book_prices_book_priceID`)
REFERENCES `books`.`book_prices` (`book_priceID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_books_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batch_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batch_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`batchID` INT(11) NOT NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_batch_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_batch_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`batches`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`batches` (
`batchID` INT(11) NOT NULL AUTO_INCREMENT,
`batch_transaction_transactionID` INT(11) NULL,
`book_typeID` INT(11) NOT NULL,
`price` DECIMAL(10,2) NULL,
`supplierID` INT(11) NULL,
PRIMARY KEY (`batchID`),
INDEX `fk_batches_batch_transaction1_idx` (`batch_transaction_transactionID` ASC),
CONSTRAINT `fk_batches_batch_transaction1`
FOREIGN KEY (`batch_transaction_transactionID`)
REFERENCES `books`.`batch_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`suppliers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`suppliers` (
`supplierID` INT(11) NOT NULL AUTO_INCREMENT,
`batches_batchID` INT(11) NOT NULL,
`name` VARCHAR(255) NULL,
PRIMARY KEY (`supplierID`),
INDEX `fk_suppliers_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_suppliers_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customer_transaction`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customer_transaction` (
`transactionID` INT(11) NOT NULL AUTO_INCREMENT,
`customerID` INT(11) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`date` DATETIME NULL,
PRIMARY KEY (`transactionID`),
INDEX `fk_client_transaction_transactions1_idx` (`transactions_transactionID` ASC),
CONSTRAINT `fk_customer_transaction_transactions1`
FOREIGN KEY (`transactions_transactionID`)
REFERENCES `books`.`transactions` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`customers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`customers` (
`customerID` INT(11) NOT NULL AUTO_INCREMENT,
`books_bookID` INT(11) NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`customer_transaction_transactionID` INT(11) NOT NULL,
PRIMARY KEY (`customerID`),
INDEX `fk_clients_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_clients_client_transaction1_idx` (`customer_transaction_transactionID` ASC),
CONSTRAINT `fk_customrs_products1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customers_customer_transaction1`
FOREIGN KEY (`customer_transaction_transactionID`)
REFERENCES `books`.`customer_transaction` (`transactionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discounts` (
`discountID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`transactions_transactionID` INT(11) NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NULL,
PRIMARY KEY (`discountID`),
INDEX `fk_discounts_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_discounts_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`discount_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`discount_types` (
`discount_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`Type` VARCHAR(255) NULL,
`discounts_discountID` INT(11) NOT NULL,
PRIMARY KEY (`discount_typeID`),
INDEX `fk_discount_types_discounts1_idx` (`discounts_discountID` ASC),
CONSTRAINT `fk_discount_types_discounts1`
FOREIGN KEY (`discounts_discountID`)
REFERENCES `books`.`discounts` (`discountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `books`.`storagedistribution`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`storagedistribution` (
`storagedistributionID` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NULL,
`transactions_transactionID` INT(11) NOT NULL,
`transactions_transaction_types_transaction_typeID` INT(11) NOT NULL,
PRIMARY KEY (`storagedistributionID`),
INDEX `fk_storagedistribution_transactions1_idx` (`transactions_transactionID` ASC, `transactions_transaction_types_transaction_typeID` ASC),
CONSTRAINT `fk_storagedistribution_transactions1`
FOREIGN KEY (`transactions_transactionID` , `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID` , `transaction_types_transaction_typeID`)
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;
And now I got the same error in the table book_types:
ERROR: Error 1005: Can't create table `books`.`book_types` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
-- -----------------------------------------------------
-- Table `books`.`book_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `books`.`book_types` (
`book_typeID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`books_bookID` INT(11) NOT NULL,
`books_book_prices_book_priceID` INT(11) NOT NULL,
`books_book_types_book_typeID` INT(11) NOT NULL,
`batches_batchID` INT(11) NOT NULL,
PRIMARY KEY (`book_typeID`),
INDEX `fk_product_types_products1_idx` (`books_bookID` ASC, `books_book_prices_book_priceID` ASC, `books_book_types_book_typeID` ASC),
INDEX `fk_product_types_batches1_idx` (`batches_batchID` ASC),
CONSTRAINT `fk_book_types_books1`
FOREIGN KEY (`books_bookID` , `books_book_prices_book_priceID` , `books_book_types_book_typeID`)
REFERENCES `books`.`books` (`bookID` , `book_prices_book_priceID` , `book_types_book_typeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_types_batches1`
FOREIGN KEY (`batches_batchID`)
REFERENCES `books`.`batches` (`batchID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 11 succeeded, 1 failed

FOREIGN KEY (`transactions_transactionID`, `transactions_transaction_types_transaction_typeID`)
REFERENCES `books`.`transactions` (`transactionID`, `transaction_types_transaction_typeID`)
Assuming you have good reasons to store transaction_typeID in the books table, though it's functionally dependend on transactionID. Any foreign key constraint needs an index in both tables (referencing/child and referenced/parent) which can support the constraint check.
If no supporting index is found in the child table (books), the engine will create one. That's why you have the following index:
INDEX `fk_books_transactions1_idx` (
`transactions_transactionID` ASC,
`transactions_transaction_types_transaction_typeID` ASC
)
But the engine will not create an index in the parent table (transactions). Indexes you already have are:
PRIMARY KEY (`transactionID`),
INDEX `fk_transactions_transaction_types1_idx` (`transaction_types_transaction_typeID` ASC),
Neither of them can support the FK constraint. So you need to define a new index in the transactions table:
INDEX (`transactionID`, `transaction_types_transaction_typeID`)
However - That is a denormalized design, which isn't something bad, if you have good reasons for it, since the consistency is enforced by the FK constraint. But if you know no good reasons for storing transaction_typeID in the books table, you should just remove it and change the FK constraint to
FOREIGN KEY (`transactions_transactionID`) REFERENCES `books`.`transactions` (`transactionID`)
Now it's referencing the primary key of the parent table. This is a basic design for foreign keys, and the constraint is supported by the PRIMARY KEY (index).

Problem solved by updating the relationships in the diagram.
The first diagram was denormalized, which isn't necessarily bad if there are justifications for it (the consistency was enforced by the FK constraint).
I ended up referencing only the PK of the "parent" table, being the _types table a parent one.
Final diagram:

Related

Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'

I'm forward engineering a database from an ERD that I created in MySqlWorkbench. I have a very basic many to many relationship between the "gyms" table and the "instructors" table, but I'm getting the error above. To me, the error reads that MySql can't find the "gym_id" column in the "gyms" table when it tries to create the many-to-many table.
I don't know why that would be the case.
I've went through the tables to make sure there were no typos and that the data types matched. I also went through the execution script to make sure the "gyms" table was being created before the many-to-many. It was. I can't sniff out what is causing the error. So any help or suggestion is appreciated
This is the error:
Executing SQL script in server
ERROR: Error 3734: Failed to add the foreign key constraint. Missing column 'gym_id' for constraint 'fk_gyms_instructors_gyms' in the referenced table 'gyms'
SQL Code:
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
The other potentially useful information is:
1. On the many to many table, in the foreign keys section both gym_id and instructor_id are SMALLINTS and NOT NULL,
2. On their respective table the gym_id and instructor_id are autoincrementing primary keys, NOT NULL
3. I pasted the full script below if you want to look.
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema fitness
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `fitness` DEFAULT CHARACTER SET utf8 ;
USE `fitness` ;
-- -----------------------------------------------------
-- Table `fitness`.`fitness_classes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`fitness_classes` (
`fitness_class_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`description` VARCHAR(500) NOT NULL,
`vacancies` INT NOT NULL,
`start_time` DATETIME NOT NULL,
PRIMARY KEY (`fitness_class_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` TEXT(1200) NULL,
PRIMARY KEY (`category_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`tags`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`category_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_tags_fitness_classes_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_tags_categories_idx` (`category_id` ASC) VISIBLE,
CONSTRAINT `fk_tags_fitness_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tags_categories`
FOREIGN KEY (`category_id`)
REFERENCES `fitness`.`categories` (`category_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms` (
`gym_id` SMALLINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(55) NOT NULL,
`address` VARCHAR(55) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`state` VARCHAR(25) NOT NULL,
`phone` VARCHAR(10) NOT NULL,
`latitude` DECIMAL(10,8) NULL,
`longitude` DECIMAL(10,8) NULL,
`neighborhood` VARCHAR(45) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`gym_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`instructors` (
`instructor_id` SMALLINT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(55) NOT NULL,
`last_name` VARCHAR(55) NOT NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`instructor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`gyms_instructors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`gyms_instructors` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_gyms_instructors_gyms_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_gyms_instructors_instructors_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_gyms_instructors_gyms`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_gyms_instructors_instructors`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`listings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`listings` (
`id` INT NOT NULL AUTO_INCREMENT,
`gym_id` SMALLINT NOT NULL,
`fitness_class_id` INT NOT NULL,
`instructor_id` SMALLINT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_listings_gym_idx` (`gym_id` ASC) VISIBLE,
INDEX `fk_listings_fitness_class_idx` (`fitness_class_id` ASC) VISIBLE,
INDEX `fk_listings_instructor_idx` (`instructor_id` ASC) VISIBLE,
CONSTRAINT `fk_listings_gym`
FOREIGN KEY (`gym_id`)
REFERENCES `fitness`.`gyms` (`gym_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_listings_fitness_class`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_listings_instructor`
FOREIGN KEY (`instructor_id`)
REFERENCES `fitness`.`instructors` (`instructor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`username` VARCHAR(16) NOT NULL,
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `fitness`.`reviews`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`reviews` (
`review_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`rating` TINYINT(1) NOT NULL,
`review_text` TEXT(1200) NULL,
`create_time` TIMESTAMP NOT NULL,
PRIMARY KEY (`review_id`),
INDEX `fk_reviews_classes_idx` (`fitness_class_id` ASC) VISIBLE,
CONSTRAINT `fk_reviews_classes`
FOREIGN KEY (`fitness_class_id`)
REFERENCES `fitness`.`fitness_classes` (`fitness_class_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fitness`.`bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fitness`.`bookings` (
`booking_id` INT NOT NULL AUTO_INCREMENT,
`fitness_class_id` INT NOT NULL,
`user_id` INT NOT NULL,
`price` DECIMAL(2,2) NOT NULL,
`class_start_time` DATETIME NOT NULL,
`purchase_date` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`booking_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `fitness`.`users` ()
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fitness_class_id`
FOREIGN KEY ()
REFERENCES `fitness`.`fitness_classes` ()
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;

Error 1215: Cannot add foreign key constraint mysql workbench 1

I'm trying to forward engineering my schema with MySQL Workbench but this error appears. I'm sure that the foreign key refers to the primary key of the parent table and their are both VARCHAR(45).
This is my schema:
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema PiscineRoma
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `PiscineRoma` ;
USE `PiscineRoma` ;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Piscina`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Piscina` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Piscina` (
`Nome` VARCHAR(45) NOT NULL,
`NumeroDiTelefono` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`NomeResponsabile` VARCHAR(45) NOT NULL,
`DataInizioDisponibilitàVascaEsterna` DATE NULL,
`DataFineDisponibilitàVascaEsterna` DATE NULL,
PRIMARY KEY (`Nome`),
UNIQUE INDEX `NumeroDiTelefono_UNIQUE` (`NumeroDiTelefono` ASC),
UNIQUE INDEX `Indirizzo_UNIQUE` (`Indirizzo` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Persona`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Persona` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Persona` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Indirizzo` VARCHAR(45) NOT NULL,
`Età` INT NOT NULL,
`Telefono` VARCHAR(20) NULL,
`Cellulare` VARCHAR(20) NULL,
`IndirizzoEMail` VARCHAR(45) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC),
UNIQUE INDEX `Cellulare_UNIQUE` (`Cellulare` ASC),
UNIQUE INDEX `IndirizzoEMail_UNIQUE` (`IndirizzoEMail` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`IngressoSingolo`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`IngressoSingolo` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`IngressoSingolo` (
`NomePiscina` VARCHAR(45) NOT NULL,
`Persona_CF` VARCHAR(16) NOT NULL,
`DataIngresso` DATETIME NOT NULL,
PRIMARY KEY (`NomePiscina`, `Persona_CF`),
INDEX `fk_Piscina_has_PersonaNonIscritta_Piscina_idx` (`NomePiscina` ASC),
INDEX `fk_IngressoSingolo_Persona1_idx` (`Persona_CF` ASC),
CONSTRAINT `fk_Piscina_has_PersonaNonIscritta_Piscina`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_IngressoSingolo_Persona1`
FOREIGN KEY (`Persona_CF`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Insegnante`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Insegnante` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Insegnante` (
`CF` VARCHAR(16) NOT NULL,
`Nome` VARCHAR(45) NOT NULL,
`Cognome` VARCHAR(45) NOT NULL,
`Telefono` VARCHAR(20) NULL,
PRIMARY KEY (`CF`),
UNIQUE INDEX `Telefono_UNIQUE` (`Telefono` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Qualifica`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Qualifica` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Qualifica` (
`Nome` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Nome`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Possiede`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Possiede` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Possiede` (
`Insegnante` VARCHAR(16) NOT NULL,
`Qualifica` VARCHAR(45) NOT NULL,
PRIMARY KEY (`Insegnante`, `Qualifica`),
INDEX `fk_Insegnante_has_Qualifica_Qualifica1_idx` (`Qualifica` ASC),
INDEX `fk_Insegnante_has_Qualifica_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Insegnante_has_Qualifica_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Insegnante_has_Qualifica_Qualifica1`
FOREIGN KEY (`Qualifica`)
REFERENCES `PiscineRoma`.`Qualifica` (`Nome`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Impiego`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Impiego` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Impiego` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`Inizio` DATE NOT NULL,
`Fine` DATE NULL,
`Tipo` VARCHAR(45) NOT NULL DEFAULT 'impiego_corrente',
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Impiego_Piscina1_idx` (`NomePiscina` ASC),
INDEX `fk_Impiego_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Impiego_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Impiego_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Corso`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Corso` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Corso` (
`NomeAttivita` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Costo` SMALLINT(2) NOT NULL,
`NumeroMinimoDiPartecipanti` SMALLINT(2) NOT NULL,
`NumeroMassimoDiPartecipanti` SMALLINT(2) NOT NULL,
PRIMARY KEY (`NomeAttivita`, `NomePiscina`),
INDEX `fk_Corso_Piscina1_idx` (`NomePiscina` ASC),
CONSTRAINT `fk_Corso_Piscina1`
FOREIGN KEY (`NomePiscina`)
REFERENCES `PiscineRoma`.`Piscina` (`Nome`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
PACK_KEYS = DEFAULT
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Lezione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Lezione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Lezione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`Insegnante` VARCHAR(16) NOT NULL,
`Dat` DATETIME NOT NULL,
`Numero` INT NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Lezione_Insegnante1_idx` (`Insegnante` ASC),
CONSTRAINT `fk_Lezione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Lezione_Insegnante1`
FOREIGN KEY (`Insegnante`)
REFERENCES `PiscineRoma`.`Insegnante` (`CF`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `PiscineRoma`.`Iscrizione`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PiscineRoma`.`Iscrizione` ;
CREATE TABLE IF NOT EXISTS `PiscineRoma`.`Iscrizione` (
`Codice` INT NOT NULL AUTO_INCREMENT,
`DataIscrizione` DATE NOT NULL,
`CodiceCertificato` INT NULL,
`DataCertificato` DATE NULL,
`Medico` VARCHAR(45) NULL,
`NomeAttivitaCorso` VARCHAR(45) NOT NULL,
`NomePiscina` VARCHAR(45) NOT NULL,
`PersonaIscritta` VARCHAR(16) NOT NULL,
PRIMARY KEY (`Codice`),
INDEX `fk_Iscrizione_Corso1_idx` (`NomeAttivitaCorso` ASC, `NomePiscina` ASC),
INDEX `fk_Iscrizione_PersonaIscritta1_idx` (`PersonaIscritta` ASC),
UNIQUE INDEX `CodiceCertificato_UNIQUE` (`CodiceCertificato` ASC),
CONSTRAINT `fk_Iscrizione_Corso1`
FOREIGN KEY (`NomeAttivitaCorso` , `NomePiscina`)
REFERENCES `PiscineRoma`.`Corso` (`NomeAttivita` , `NomePiscina`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `fk_Iscrizione_PersonaIscritta1`
FOREIGN KEY (`PersonaIscritta`)
REFERENCES `PiscineRoma`.`Persona` (`CF`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
USE `PiscineRoma` ;
The message log from MySQL Workbench tells that error is in the PiscineRoma.Corso.
I also tryed to run the command SHOW ENGINE INNODB STATUS and under the label the error is:
2019-02-14 19:52:03 0x7f9fa9700700 Error in foreign key constraint of table PiscineRoma/Iscrizione:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT `fk_Iscrizione_Corso1` FOREIGN KEY (`NomeAttivita`, `NomePiscina`) REFERENCES `Corso` (`NomeAttività`, `NomePiscina`) ON DELETE NO ACTION ON UPDATE CASCADE
The index in the foreign key in table is fk_Iscrizione_Corso1_idx
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
Thank you all for answers.

My SQL Duplicated key

I'm creating a database with mysql and I have a problem with duplicated key, I don't find the error but I know which is the table error, I think the problem is with the FK's in "historiaLaboral" table the SP is this:
Im using mysql workbench I would like to you advising me how improve the model
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SGD
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SGD` DEFAULT CHARACTER SET utf8 ;
USE `SGD` ;
-- -----------------------------------------------------
-- Table `SGD`.`tipoIdentificación`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`tipoIdentificación` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empresa`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empresa` (
`id` INT NOT NULL,
`razonSocial` VARCHAR(45) NOT NULL,
`NIT` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`direccion` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`empleado`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`empleado` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`id_tipoDocumento` INT NOT NULL,
`numeroDocumento` INT(20) NULL,
`aspiracionSalarial` DOUBLE NOT NULL,
`telefono` INT NOT NULL,
`direccion` VARCHAR(30) NOT NULL,
`id_empresa` INT NOT NULL,
`genero` ENUM('F', 'M') NOT NULL,
`aspirante` BIT(1) NULL,
PRIMARY KEY (`id`),
INDEX `id_tipoDocumento_idx` (`id_tipoDocumento` ASC),
INDEX `id_empresa_idx` (`id_empresa` ASC),
CONSTRAINT `id_tipoDocumento`
FOREIGN KEY (`id_tipoDocumento`)
REFERENCES `SGD`.`tipoIdentificación` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_empresa`
FOREIGN KEY (`id_empresa`)
REFERENCES `SGD`.`empresa` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`rol`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`rol` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`usuario` (
`id` INT NOT NULL AUTO_INCREMENT,
`usuario` VARCHAR(45) NOT NULL,
`contrasena` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`id_empleado` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`historiaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`historiaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_empleado` INT NOT NULL,
`id_usuarioRegistra` INT NOT NULL,
`fechaIngreso` DATE NOT NULL,
`fechaModificacion` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_empleado_idx` (`id_empleado` ASC),
INDEX `id_usuarioRegistra_idx` (`id_usuarioRegistra` ASC),
CONSTRAINT `id_empleado`
FOREIGN KEY (`id_empleado`)
REFERENCES `SGD`.`empleado` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_usuarioRegistra`
FOREIGN KEY (`id_usuarioRegistra`)
REFERENCES `SGD`.`usuario` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`referencia`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`referencia` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombres` VARCHAR(45) NOT NULL,
`apellidos` VARCHAR(45) NOT NULL,
`relacion` VARCHAR(45) NOT NULL,
`telefono` VARCHAR(45) NOT NULL,
`telefono_2` VARCHAR(45) NULL,
`id_HLaboral` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`experienciaLaboral`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`experienciaLaboral` (
`id` INT NOT NULL AUTO_INCREMENT,
`empresa` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`motivoRetiro` VARCHAR(45) NOT NULL,
`id_HLaboral` INT NOT NULL,
`certificado` BIT(1) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_Empleado_idx` (`id_HLaboral` ASC),
CONSTRAINT `id_Empleado`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`estudios`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`estudios` (
`id` INT NOT NULL AUTO_INCREMENT,
`institución` VARCHAR(45) NOT NULL,
`fechaInicio` DATE NOT NULL,
`fechaFin` DATE NOT NULL,
`certificado` BIT(1) NOT NULL DEFAULT 0,
`id_HLaboral` INT NOT NULL,
`id_tipoEstudio` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_tipoEstudio` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_tipoEstudio`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`serie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`serie` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`soporte`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`soporte` (
`id` INT NOT NULL,
`papel` BIT(1) NOT NULL,
`digital` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`retencion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`retencion` (
`id` INT NOT NULL AUTO_INCREMENT,
`archivoGestion` INT NOT NULL,
`archivoCentral` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`dispFinal`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`dispFinal` (
`id` INT NOT NULL AUTO_INCREMENT,
`conservacionTotal` BIT(1) NULL,
`eliminacion` BIT(1) NULL,
`digitalizacion` BIT(1) NULL,
`seleccion` BIT(1) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`TRD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`TRD` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`detalle` VARCHAR(2000) NOT NULL,
`id_soporte` INT NOT NULL,
`id_retencion` INT NOT NULL,
`id_dispFinal` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_serie` ASC),
INDEX `id_soporte_idx` (`id_soporte` ASC),
INDEX `id_retencion_idx` (`id_retencion` ASC),
INDEX `id_dispFinal_idx` (`id_dispFinal` ASC),
CONSTRAINT `id_serie`
FOREIGN KEY (`id_serie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_soporte`
FOREIGN KEY (`id_soporte`)
REFERENCES `SGD`.`soporte` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_retencion`
FOREIGN KEY (`id_retencion`)
REFERENCES `SGD`.`retencion` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_dispFinal`
FOREIGN KEY (`id_dispFinal`)
REFERENCES `SGD`.`dispFinal` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`documento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`documento` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_HLaboral` INT NOT NULL,
`id_TRD` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_HLaboral_idx` (`id_HLaboral` ASC),
INDEX `id_TRD_idx` (`id_TRD` ASC),
CONSTRAINT `id_HLaboral`
FOREIGN KEY (`id_HLaboral`)
REFERENCES `SGD`.`historiaLaboral` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `id_TRD`
FOREIGN KEY (`id_TRD`)
REFERENCES `SGD`.`TRD` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`ubicacion`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`ubicacion` (
`id` INT NOT NULL AUTO_INCREMENT,
`analquel` VARCHAR(45) NOT NULL,
`estante` VARCHAR(45) NOT NULL,
`id_documento` INT NULL,
PRIMARY KEY (`id`),
INDEX `id_documento_idx` (`id_documento` ASC),
CONSTRAINT `id_documento`
FOREIGN KEY (`id_documento`)
REFERENCES `SGD`.`documento` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`subserie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`subserie` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_serie` INT NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`id_subSerie` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_serie_idx` (`id_subSerie` ASC),
CONSTRAINT `id_subSerie`
FOREIGN KEY (`id_subSerie`)
REFERENCES `SGD`.`serie` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SGD`.`permisos`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SGD`.`permisos` (
`id` INT NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NOT NULL,
`id_rol` INT NOT NULL,
`acceso` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `id_rol_idx` (`id_rol` ASC),
CONSTRAINT `id_rol`
FOREIGN KEY (`id_rol`)
REFERENCES `SGD`.`rol` (`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;
I'm based on this model
Thanks for helping me!

Unknown datatype on fk reference

I am trying to run this mysql commands in my H2 database. The DB is set to mysql mode.
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL,
`username` VARCHAR(45) NOT NULL COMMENT ' ',
`email` VARCHAR(320) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `serverprojects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `serverprojects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`status` TINYINT(1) NULL,
`restorescript_relative_path` VARCHAR(255) NULL,
`backupscript_relative_path` VARCHAR(255) NULL,
`installscript_relative_path` VARCHAR(255) NULL,
`username` VARCHAR(100) NULL,
`password` VARCHAR(100) NULL,
`gradle_relative_path` VARCHAR(255) NULL,
`root_path` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `projects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`serverprojects_id` INT NOT NULL,
`users_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
CONSTRAINT `fk_projects_serverprojects`
FOREIGN KEY (`serverprojects_id`)
REFERENCES `serverprojects` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_users1`
FOREIGN KEY (`users_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET FOREIGN_KEY_CHECKS=1;
This produced the following error output
Unbekannter Datentyp: "FK_PROJECTS_SERVERPROJECTS_IDX"
Unknown data type: "FK_PROJECTS_SERVERPROJECTS_IDX"; SQL statement:
-- -----------------------------------------------------
-- Table `projects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projects` (
`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`serverprojects_id` INT NOT NULL,
`users_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
CONSTRAINT `fk_projects_serverprojects`
FOREIGN KEY (`serverprojects_id`)
REFERENCES `serverprojects` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_users1`
FOREIGN KEY (`users_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB [50004-193] HY004/50004 (Help)
Does anyone know whats going on here ? I think the problem is inside the syntax of INDEX fk_projects_serverprojects_idx (serverprojects_id ASC), but i cant get this to work
I just found out that the problem is this:
INDEX `fk_projects_serverprojects_idx` (`serverprojects_id` ASC),
INDEX `fk_projects_users1_idx` (`users_id` ASC),
in combination with the InnoDB. With InnoCb you are not alowed to do this.

MySQL Server Error 1005

When I try to run SQL script I get the log error in Workbench Forward Engineer:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'regional_budget.budget' (errno: 121)
SQL Code:
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 13 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
SQL script:
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema regional_budget
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema regional_budget
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `regional_budget` DEFAULT CHARACTER SET utf8 ;
USE `regional_budget` ;
-- -----------------------------------------------------
-- Table `regional_budget`.`regions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`regions` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`subjects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`subjects` (
`id` INT NOT NULL,
`name` VARCHAR(20) NULL,
`region_id` INT NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`income`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`income` (
`id` INT NOT NULL AUTO_INCREMENT,
`tax_and_non_tax` DECIMAL(11,2) NULL,
`receipts` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`outcome`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`outcome` (
`id` INT NOT NULL AUTO_INCREMENT,
`transfers` DECIMAL(11,2) NULL,
`subventions` DECIMAL(11,2) NULL,
`subsidies` DECIMAL(11,2) NULL,
`dotations` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`population`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`population` (
`subject_id` INT NOT NULL,
`count` DECIMAL(11,2) NULL,
PRIMARY KEY (`subject_id`),
CONSTRAINT `subject_id`
FOREIGN KEY (`subject_id`)
REFERENCES `regional_budget`.`subjects` (`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;
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
P.S. I tried to create 'regional_budget'.'budget' with foreign keys after other reference tables. Also I tried to use foreign_key_checks, but nothing helps.
I suggest you try changing the identifiers of the foreign key constraints to be unique, and not conflict with identifiers already used for columns (or other constraints.
For example:
CONSTRAINT `FK_budget_regions` FOREIGN KEY ...
CONSTRAINT `FK_budget_income` FOREIGN KEY ...
CONSTRAINT `FK_budget_outcome` FOREIGN KEY ...