Error "Cannot add foreign key constraint" - mysql

I am suffering the same "Cannot add foreign key constraint" that other folks around here.
The table client_partners contains a relationship between users. The error is raised at creating client_partners.
I have checked that users.id have the same type than client_partners.clientid and client_partners.partnerid: INT UNSIGNED NOT NULL.
The configuration options are:
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';
Users table definition:
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `users` ;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(200) NOT NULL,
`salt` VARCHAR(100) NULL,
`firstname` VARCHAR(50) NOT NULL,
`lastname` VARCHAR(50) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`role` VARCHAR(10) NOT NULL DEFAULT 'supplier',
`destination` VARCHAR(10) NULL,
`supplierId` INT UNSIGNED NULL,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
INDEX `fk_users_suppliers_idx` (`supplierId` ASC),
INDEX `fk_users_roles_idx` (`role` ASC),
INDEX `fk_users_destinations1_idx` (`destination` ASC),
CONSTRAINT `fk_users_suppliers`
FOREIGN KEY (`supplierId`)
REFERENCES `suppliers` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_roles`
FOREIGN KEY (`role`)
REFERENCES `roles` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_destinations1`
FOREIGN KEY (`destination`)
REFERENCES `destinations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Client_partners table definition:
-- -----------------------------------------------------
-- Table `client_partners`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `client_partners` ;
CREATE TABLE IF NOT EXISTS `client_partners` (
`clientid` INT UNSIGNED NOT NULL,
`partnerid` INT UNSIGNED NOT NULL,
PRIMARY KEY (`clientid`, `partnerid`),
CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The datatypes (except autoincrement) are the same. The referencing table (users) is created first. What am I missing?

CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
here you are referencing to id of table users twice..it is not possible. try by removing one and create
If you want to reference to another foreign key you need to create it by using another name.

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;

MySQL Workbench Error 1005 cant create table

So I am very new to MySQL but I would like to think I that grasp a decent bit of it so far. I am struggling with the creation of the table PROJECT. I'm almost positive after my own research that it has something to do with my keys in the table. But I do not understand where the problem is or how to fix it.
The SQL queries below creates all tables for the project. I'm not sure of the problem because I only get the error code for the project for now. Any insight and advice on what causes this and how to rectify this in the code would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`department`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC),
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dept_locations`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dept_locations` (
`dnumber` INT(11) NOT NULL,
`dlocation` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dnumber`, `dlocation`),
CONSTRAINT `dnumber`
FOREIGN KEY (`dnumber`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`project`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`project` (
`pnumber` INT(11) NOT NULL,
`pname` VARCHAR(45) NULL,
`plocation` VARCHAR(45) NULL,
`dnum` INT(11) NOT NULL,
PRIMARY KEY (`pnumber`),
INDEX `dnum_idx` (`dnum` ASC),
INDEX `plocation_idx` (`plocation` ASC),
CONSTRAINT `dnum`
FOREIGN KEY (`dnum`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `ebrasi1db`.`dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`works_on`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`works_on` (
`essn` VARCHAR(10) NOT NULL,
`pno` INT NOT NULL,
`hours` DECIMAL(5,2) NULL,
PRIMARY KEY (`essn`, `pno`),
INDEX `pno_idx` (`pno` ASC),
CONSTRAINT `works_on_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `pno`
FOREIGN KEY (`pno`)
REFERENCES `ebrasi1db`.`project` (`pnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dependent`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dependent` (
`essn` VARCHAR(10) NOT NULL,
`dependent_name` VARCHAR(45) NOT NULL,
`sex` VARCHAR(1) NULL,
`bdate` DATE NULL,
`relation` VARCHAR(45) NULL,
PRIMARY KEY (`essn`, `dependent_name`),
CONSTRAINT `dependent_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Your problem lies essentially in the fact that you are trying to create a constraint to a non key column (because the referenced table has a double key).
In your table project you have:
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
dept_locations.dlocation is not a key ALONE (primary key), therefore the reason you are not able to create that constraint.
You need to make the constraint for both keys from the referenced table like this:
CONSTRAINT `plocation`
FOREIGN KEY (`pnumber`, `plocation`)
REFERENCES `dept_locations` (`dnumber`, `dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
In this case you won't need the first constraint dnum as it is already referenced in dept_locations table.
Also note that you tables department and employee will not be created because one reference the other so you need to create the tables first without the constraints then apply the constraints like:
ALTER TABLE `employee` add
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Same for the department table and its constraint to employee
Normally you get the Error 1005 when it unable to create the foreign keys. When you are creating a foreign key constraint the parent table should be available in the database.
In your case 'employee' table has a FK to 'department' table but you are trying to create the 'employee' table before creating the 'department' table. Also the 'department' table has a FK back to 'employee' table. Therefore you are not able to create the 'department' table first. To solve this you may create the 'department' table first without FK to 'employee' table. Then create the 'employee' table and then alter the 'department' table with FK.
Here is the modified code for first two tables and follow the same pattern for rest if you get the same error.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
alter table department add
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

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 Data Import - Cannot add foreign key constraint error,

I've created a model and forward engineered to SQL text for importing. When I start the data import to an existing connection, I receive the following error,
ERROR 1215 (HY000) at line 76: Cannot add foreign key constraint
I've checked for the data type of the parent and the child and etc but not sure where the error comes from..
Using MySQLWorkbench 6.3 with MySQL 5.7
Line 76 is the beginning of the following extract,
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Whole SQL Text,
-- MySQL Script generated by MySQL Workbench
-- 04/11/16 12:31:19
-- Model: New Model Version: 1.0
-- 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 CostTrackerDB
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema CostTrackerDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `CostTrackerDB` DEFAULT CHARACTER SET utf8 ;
USE `CostTrackerDB` ;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Staff`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Staff` (
`idStaff` INT NOT NULL,
`Name` VARCHAR(45) NULL,
`Role` VARCHAR(45) NULL,
PRIMARY KEY (`idStaff`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Client` (
`idClient` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`idClient`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Job`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Job` (
`idJob` INT NOT NULL,
`Number` VARCHAR(45) NULL,
`Title` VARCHAR(100) NULL,
`State` VARCHAR(45) NULL,
`Start` DATETIME NULL,
`Due` DATETIME NULL,
`Client_idClient` INT NOT NULL,
PRIMARY KEY (`idJob`),
INDEX `fk_Job_Client1_idx` (`Client_idClient` ASC),
CONSTRAINT `fk_Job_Client1`
FOREIGN KEY (`Client_idClient`)
REFERENCES `CostTrackerDB`.`Client` (`idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Task`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Task` (
`idTask` INT NOT NULL,
`Title` VARCHAR(100) NULL,
`idStaff` INT NULL,
PRIMARY KEY (`idTask`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Expense`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`StaffAllocation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`StaffAllocation` (
`Job_idJob` INT NOT NULL,
`Staff_idStaff` INT NOT NULL,
`Task_idTask` INT NOT NULL,
`Rate` DOUBLE NULL,
`Role` VARCHAR(45) NULL,
`Hours` DOUBLE NULL,
`Week` DATETIME NULL,
PRIMARY KEY (`Job_idJob`, `Staff_idStaff`, `Task_idTask`),
INDEX `fk_Job_has_Staff_Staff1_idx` (`Staff_idStaff` ASC),
INDEX `fk_Job_has_Staff_Job1_idx` (`Job_idJob` ASC),
INDEX `fk_StaffAllocation_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Job_has_Staff_Job1`
FOREIGN KEY (`Job_idJob`)
REFERENCES `CostTrackerDB`.`Job` (`idJob`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Job_has_Staff_Staff1`
FOREIGN KEY (`Staff_idStaff`)
REFERENCES `CostTrackerDB`.`Staff` (`idStaff`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_StaffAllocation_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CostTrackerDB`.`Timesheet`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Timesheet` (
`Task_idTask` INT NOT NULL,
`Staff_idStaff` INT NOT NULL,
`Time` DATETIME NULL,
`Billable` TINYINT(1) NULL,
`hours` DATETIME NULL,
PRIMARY KEY (`Task_idTask`, `Staff_idStaff`),
INDEX `fk_Task_has_Staff_Staff1_idx` (`Staff_idStaff` ASC),
INDEX `fk_Task_has_Staff_Task1_idx` (`Task_idTask` ASC),
CONSTRAINT `fk_Task_has_Staff_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Task_has_Staff_Staff1`
FOREIGN KEY (`Staff_idStaff`)
REFERENCES `CostTrackerDB`.`Staff` (`idStaff`)
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;
Try:
CREATE TABLE IF NOT EXISTS `CostTrackerDB`.`Expense` (
`idExpense` INT NOT NULL,
`Date` DATETIME NULL,
`Description` VARCHAR(100) NULL,
`Amount` DOUBLE NULL,
`Billable` TINYINT(1) NULL,
`Job_idJob` INT NOT NULL,
`Job_idClient` INT NOT NULL,
`Task_idTask` INT NOT NULL,
PRIMARY KEY (`idExpense`),
INDEX `fk_Expense_Job1_idx` (`Job_idJob` ASC, `Job_idClient` ASC),
INDEX `fk_Expense_Task1_idx` (`Task_idTask` ASC),
/*CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob` , `Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`idJob` , `Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,*/
CONSTRAINT `fk_Expense_Job1`
FOREIGN KEY (`Job_idJob`)
REFERENCES `CostTrackerDB`.`Job` (`idJob`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Job2`
FOREIGN KEY (`Job_idClient`)
REFERENCES `CostTrackerDB`.`Job` (`Client_idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Expense_Task1`
FOREIGN KEY (`Task_idTask`)
REFERENCES `CostTrackerDB`.`Task` (`idTask`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

MySQL Error code: 1215. Cannot add foreign key contraint. I cannot see the error

I've got the famous 1215 Error but I can not find the reason why...
I've already seen several topics as MySQL Error 1215: Cannot add foreign key constraint , and I followed all their tips, but I don't see the error....
(Squemas are automatically generated by MySQL Workbench...)
With the following structures, for the table "modalidad", I got:
Error code 1215: Cannot add foreign key constraint
Code:
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';
DROP TABLE IF EXISTS `qvh056`.`centro` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`centro` (
`id` INT NOT NULL COMMENT 'codigo',
`nombre` VARCHAR(45) NULL,
`orden` INT NULL,
`activo` TINYINT(1) NULL DEFAULT 1,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
DROP TABLE IF EXISTS `qvh056`.`compania` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`compania` (
`id` INT NOT NULL,
`codigo` INT NOT NULL COMMENT 'Codigo contable. Se puede identificar a la compaƱia con empresa y codigo.',
`nombre` VARCHAR(100) NOT NULL,
`alias` VARCHAR(100) NULL,
`centro_id` INT NOT NULL DEFAULT 0 COMMENT '\"empresa\"',
PRIMARY KEY (`id`),
INDEX `fk_compania_centro1_idx` (`centro_id` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
CONSTRAINT `fk_compania_centro1`
FOREIGN KEY (`centro_id`)
REFERENCES `qvh056`.`centro` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
DROP TABLE IF EXISTS `qvh056`.`modalidad` ;
CREATE TABLE IF NOT EXISTS `qvh056`.`modalidad` (
`id` CHAR(4) NOT NULL COMMENT 'codigo + centro\n',
`codigo` CHAR(1) NOT NULL,
`nombre` VARCHAR(45) NOT NULL,
`centro_id` INT NOT NULL,
`modality` CHAR(2) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
INDEX `fk_modalidad_centro1_idx` (`centro_id` ASC),
CONSTRAINT `fk_modalidad_centro1`
FOREIGN KEY (`centro_id`)
REFERENCES `qvh056`.`centro` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;