Mysql errno 150. I do not understand what is missing - mysql

The provinces table
CREATE TABLE IF NOT EXISTS `shmarket`.`PROVINCES` (
`provinceId` VARCHAR(6) NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`provinceId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
The cities table
Table `shmarket`.`CITIES`
CREATE TABLE IF NOT EXISTS `shmarket`.`CITIES` (
`cityId` VARCHAR(6) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`PROVINCES_provinceId` VARCHAR(6) NOT NULL,
PRIMARY KEY (`cityId`),
INDEX `fk_CITIES_PROVINCES1_idx` (`PROVINCES_provinceId` ASC),
CONSTRAINT `fk_CITIES_PROVINCES1`
FOREIGN KEY (`PROVINCES_provinceId`)
REFERENCES `shmarket`.`PROVINCES` (`provinceId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-----------------------------------------------------
Table `shmarket`.`USERS`
-----------------------------------------------------
CREATE TABLE IF NOT EXISTS `shmarket`.`USERS` (
`userId` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`surname` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`registryDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`role` VARCHAR(6) NOT NULL,
`CITIES_cityId` VARCHAR(6) NOT NULL,
PRIMARY KEY (`userId`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
INDEX `fk_USERS_CITIES1_idx` (`CITIES_cityId` ASC),
CONSTRAINT `fk_USERS_CITIES1`
FOREIGN KEY (`CITIES_cityId`)
REFERENCES `shmarket`.`CITIES` (`cityId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
When i create the users table mysql shows errno 150. I checked the code few times but its not working. I generated this code with mysql workbench and also is failing.

If you're using the InnoDB engine, the ON DELETE SET DEFAULT is your problem. Here's an excerpt from the manual:
While SET DEFAULT is allowed by the MySQL Server, it is rejected as invalid by InnoDB. CREATE TABLE and ALTER TABLE statements using this clause are not allowed for InnoDB tables.
You can use ON DELETE CASCADE or ON DELETE SET NULL, but not ON DELETE SET DEFAULT. There's more information here.
This error also occurs if you are relating columns of different types, eg. int in the source table and BigInt in the destination table.
You can run
SHOW ENGINE INNODB STATUS

Related

foreign key constraint is incorrectly formed, but both foreign and reference key column are identical

I was doing a "reverse engineer" from MySQL Workbench to a MariaDB database. When I perform reverse engineer, I got an error of "foreign key constraint is incorrectly formed" on the pivot table, even though both reference and foreign keys has identitcal properties and attributes.
I have 3 tables with 2 being independent tables that has many-to-many relationship using pivot table. The tables are users table, certificates table, and users_has_certificates pivot table. Both users and certificates tables uses id with identitcal types, that is BIGINT(20), NOT NULL, UNSIGNED, and AUTO_INCREMENT. However, I get the error of constraint is incorrectly formed when generating the pivot table.
When running SHOW ENGINE INNODB STATUS, it displays this error specifically.
LATEST FOREIGN KEY ERROR
------------------------
2021-03-15 21:09:31 0x5a8 Error in foreign key constraint of table `database_this`.`if`:
FOREIGN KEY (`certificate_id`)
REFERENCES `database_this`.`certificates` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/
for correct foreign key definition.
Create table `database_this`.`if` with foreign key constraint failed.
Field type or character set for column 'certificate_id' does not
mach referenced column 'id' near '
FOREIGN KEY (`certificate_id`)
REFERENCES `database_this`.`certificates` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci'.
Here is the SQL code for generating each tables.
users table:
CREATE TABLE IF NOT EXISTS `database_this`.`users` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `users_name_unique` (`name` ASC),
UNIQUE INDEX `users_email_unique` (`email` ASC))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
certificates table:
CREATE TABLE IF NOT EXISTS `database_this`.`certificates` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`desc` TEXT NULL DEFAULT NULL,
`certifiable_type` VARCHAR(255) NOT NULL,
`certifiable_id` BIGINT(20) UNSIGNED NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP(),
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP(),
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4;
users_has_certificates table:
CREATE TABLE IF NOT EXISTS `database_this`.`users_has_certificates` (
`user_id` BIGINT(20) UNSIGNED NOT NULL,
`certificate_id` BIGINT(20) UNSIGNED NOT NULL,
`expired_date` DATE NULL,
INDEX `fk_users_has_certificates_certificates1_idx` (`certificate_id` ASC),
INDEX `fk_users_has_certificates_users1_idx` (`user_id` ASC),
CONSTRAINT `fk_users_has_certificates_users1`
FOREIGN KEY (`user_id`)
REFERENCES `database_this`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_certificates_certificates1`
FOREIGN KEY (`certificate_id`)
REFERENCES `database_this`.`certificates` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
Apparently, MySQL Workbench didn't like reverse engineering some tables if there are already similiar tables inside the original database. The solution is to drop all tables inside the database so that the database is empty, then perform reverse engineer to that database. I'll mark this one as resolved.

MYSQL Cant not create table errno: 150 Foreign key constraint is incorrectly formed

A problem occurred in the foreign key while creating a table with mysql workbench.
When i create table with mysql workbench,
workbench give me this error :
Executing SQL script in server
ERROR: Error 1005: Can't create table kipit.unlike_game_library (errno: 150 "Foreign key constraint is incorrectly formed")
Executing SQL script in server
ERROR: Error 1005: Can't create table `kipit`.`unlike_game_library` (errno: 150 "Foreign key constraint is incorrectly formed")
SQL Code:
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci
SQL script execution finished: statements: 11 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Here is my query:
-- 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 mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `kipit`;
-- -----------------------------------------------------
-- Schema kipit
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `kipit` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `kipit`;
-- -----------------------------------------------------
-- Table `kipit`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `kipit`.`game`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC),
UNIQUE INDEX `name_UNIQUE` (`name` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `kipit`.`unlike_game_library`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC),
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC),
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
SET SQL_MODE = #OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS = #OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS = #OLD_UNIQUE_CHECKS;
i check same type and unicode setting and unique key, but it's not work
here is my db fiddle result (full query) :
https://www.db-fiddle.com/f/t2x2zvcVZZqxjsBsM7UXMF/3
+ INNO STATUS
2020-04-28 11:43:42 0x1d68 Error in foreign key constraint of table `kipit`.`if`:
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table `kipit`.`if` with foreign key constraint failed. Field type or character set for column 'user_name' does not mach referenced column 'name' near '
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci'.
Seems like 'DEFAULT CHAR SET' and 'COLLATION' doesn't match. How about making it same for all table being used.
DROP TABLE IF EXISTS `kipit`.`user`;
CREATE TABLE IF NOT EXISTS `kipit`.`user`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`picture` VARCHAR(100) NOT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC)
)
ENGINE = InnoDB
AUTO_INCREMENT = 34
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`game`;
CREATE TABLE IF NOT EXISTS `kipit`.`game`
(
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`genre` VARCHAR(100) NULL,
`publisher` VARCHAR(255) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `slug_UNIQUE` (`slug` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `kipit`.`unlike_game_library`;
CREATE TABLE IF NOT EXISTS `kipit`.`unlike_game_library`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL DEFAULT now(),
`updated_at` DATETIME NULL DEFAULT now(),
`user_name` VARCHAR(100) NOT NULL,
`game_slug` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_name_UNIQUE` (`user_name` ASC) ,
UNIQUE INDEX `game_slug_UNIQUE` (`game_slug` ASC) ,
CONSTRAINT `fk_rated_game_library_user`
FOREIGN KEY (`user_name`)
REFERENCES `kipit`.`user` (`name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_rated_game_library_game1`
FOREIGN KEY (`game_slug`)
REFERENCES `kipit`.`game` (`slug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

MySQL Workbench Foreign key constraint is incorrectly formed

I have been fiddeling with MySQL Workbench as a learning project and tried to create a table with foreign keys.
The Keys reference other tables in my database.
The ID's which are beeing referenced are all of the same type "INT(11)".
All the ID's are primary Keys and are not null.
Yet I still cant forward Engineer my Database, and the following error is beeing thrown: "ERROR: Error 1005: Can't create table testcrm.role_user (errno: 150 "Foreign key constraint is incorrectly formed")"
-- Table `testcrm`.`role_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`role_user` (
`role_id` INT(11) UNSIGNED NOT NULL,
`user_id` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`role_id`, `user_id`),
INDEX `user_id_idx` (`user_id` ASC),
CONSTRAINT `role_user_role_id`
FOREIGN KEY (`role_id`)
REFERENCES `testcrm`.`roles` (`id`)
ON DELETE CASCADE
ON UPDATE RESTRICT,
CONSTRAINT `role_user_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `testcrm`.`users` (`id`)
ON DELETE CASCADE
ON UPDATE RESTRICT)
ENGINE = InnoDB
Edit: Those are my Users and Roles Tables:
-- -----------------------------------------------------
-- Table `testcrm`.`roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`roles` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `testcrm`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`users` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`email` VARCHAR(191) NOT NULL,
`email_verified_at` TIMESTAMP NULL DEFAULT NULL,
`password` VARCHAR(191) NOT NULL,
`remember_token` VARCHAR(100) NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
UNIQUE INDEX `users_email_unique` (`email` ASC),
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

Mysql error: 1215 Cannot add foreign key constraint[ foreign keys same type, innodb ]

I need a fresh pair of eyes to see what exactly I am doing wrong here.
CREATE TABLE IF NOT EXISTS `spring_normalize`.`users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `spring_normalize`.`offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `spring_normalize`.`users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
From other people who has the same problem:
Is Db InnoDB? Yes
Are all tables InnoDB ? Yes
Is unique index present on referencing table ? Yes
Are referenced and referencing column exactly of the same type ? Yes
Question what do am I doing wrong? Thanks in advance!
As the others stated in comments, your queries are correct
Couple of things you can try :
Select your database first and remove it from your 2 create table
USE `spring_normalize`;
CREATE TABLE IF NOT EXISTS `users` (
`username` VARCHAR(60) NOT NULL,
`password` VARCHAR(80) NULL,
`authority` VARCHAR(45) NULL,
`name` VARCHAR(100) NULL,
`enabled` TINYINT(1) NULL,
`email` VARCHAR(60) NULL,
PRIMARY KEY (`username`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `offers` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` VARCHAR(100) NULL,
`users_username` VARCHAR(60) NOT NULL,
PRIMARY KEY (`id`, `users_username`),
INDEX `fk_offers_users_idx` (`users_username` ASC),
CONSTRAINT `fk_offers_users`
FOREIGN KEY (`users_username`)
REFERENCES `users` (`username`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Try to specify this before the create tables in case the engine would (weirdly) try to create the table offers before users:
-- Do not check foreign key constraints
SET FOREIGN_KEY_CHECKS = 0;
and finally if nothing solves it, do this after having received the error, it will give you more info
SHOW ENGINE INNODB STATUS;

InnoDB MySql Error 1215 Cannot add foreign key constraint / even after removing cascade obligations on DELETE

I'm trying to add foreign keys to my two tables Problem and File.
These are the CREATE queries.
And both tables are created successfully:
CREATE TABLE lpsolve.tbl_file (
id INT(11) NOT NULL AUTO_INCREMENT,
problem_id INT(11) DEFAULT NULL,
title VARCHAR(255) DEFAULT NULL,
description VARCHAR(255) DEFAULT NULL,
type VARCHAR(255) DEFAULT NULL,
create_time DATETIME DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
CREATE TABLE lpsolve.tbl_probem (
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(255) DEFAULT NULL,
description VARCHAR(255) DEFAULT NULL,
create_time DATETIME DEFAULT NULL,
number_of_files INT(11) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
But when I try to add the following constraint I get the Error 1215 Error. This is the add constraint query:
ALTER TABLE tbl_file
ADD CONSTRAINT fk_problem_file
FOREIGN KEY (problem_id)
REFERENCES tbl_problem(id)
ON DELETE CASCADE
ON UPDATE RESTRICT
The query does not work even by removing the ON DELETE CASCADE and ON UPDATE RESTRICT either. There are a couple of similar problems mentioned in Stack Overflow but they did not answer my problem unfortunately.
Error message:
Cannot add foreign key constraint
The table name is tbl_probem
CREATE TABLE lpsolve.tbl_probem (
but the ALTER command is trying to reference a table:tbl_problem
REFERENCES tbl_problem(id)
Delete l from problem to solve this problem.