MySQL foreign key error 1005 - mysql

I have this error when I try to create a table: Can't create table users_groups (errno: 150)
My script:
CREATE TABLE `users_groups` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`group_id` mediumint(8) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_users_groups_users1_idx` (`user_id`),
KEY `fk_users_groups_groups1_idx` (`group_id`),
CONSTRAINT `uc_users_groups` UNIQUE (`user_id`, `group_id`),
CONSTRAINT `fk_users_groups_users1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `fk_users_groups_groups1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Error 150 is related to the definition of foreign keys.
You did not mention the MySQL version, but here are a few things to check:
tables should exist, and their storage engine should all be InnoDB
referenced columns should exist
indexes on referenced columns should exist in the referenced tables
definition of the index (including column order) should match the definition of the foreign key
data types of referenced columns should be identical (including length, null values, etc ...)
The documentation of MySQL says it is possible to use the "SHOW ENGINE INNODB STATUS" command just after error 150 to get more information about the error itself. I suggest you try it. In the output, search for the 'LATEST FOREIGN KEY ERROR' section.

Related

MySQL inconsistently altering name of indexes associated with foreign keys on InnoDB tables

I seem to have encountered an inconsistency in the way an ALTER TABLE statement behaves when dropping and adding a foreign key. Sometimes the associated index will be renamed and other times it isn't. I have identified the situations under which this occurs:
APPROACH #1
A simple person table with an auto-incrementing primary key id and a foreign key column to itself self_id. Note: the behaviour would be the same for two separate tables, I have used one table to simplify the example.
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`self_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `self_id_fk` (`self_id`),
CONSTRAINT `self_id_fk` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Next I rename the foreign key by dropping the existing one and then adding a new one. This is done in a single statement but the behaviour is the same if split into multiple ALTER TABLE statements.
ALTER TABLE `person`
DROP FOREIGN KEY `self_id_fk`,
ADD CONSTRAINT `a_new_fk_name` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`);
After this statement the table is as follows:
CREATE TABLE `person` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`self_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `self_id_fk` (`self_id`),
CONSTRAINT `a_new_fk_name` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Notice that the foreign key has been renamed but the index has not.
APPROACH #2
An alternative way of doing this would be to first create the table without any foreign keys or indexes:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`self_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Next add the foreign key constraint:
ALTER TABLE `person` ADD CONSTRAINT `self_id_fk` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`);
This results in the following table:
CREATE TABLE `person` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`self_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `self_id_fk` (`self_id`),
CONSTRAINT `self_id_fk` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Note that an index is automatically created due to the behaviour described in the documentation
... an index is created on the referencing table automatically if it
does not exist
Next I rename the foreign key in the same way as APPROACH #1:
ALTER TABLE `person`
DROP FOREIGN KEY `self_id_fk`,
ADD CONSTRAINT `a_new_fk_name` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`);
But this time both the foreign key and the index have been renamed:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`self_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `a_new_fk_name` (`self_id`),
CONSTRAINT `a_new_fk_name` FOREIGN KEY (`self_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Is there any explanation into what's going on? It's almost as if MySQL is tracking which indexes are "auto created" and then renaming them when the foreign key is changed. The Table DDL is identical for both approaches before the ALTER TABLE statement is run so there must be some "internal engine state" that MySQL is tracking.
Looking at the DDL alone there is no way to predict in which way MySQL will behave when the ALTER TABLE statement is run. This means that two "schema identical" databases could end up with mismatched schema once a simple ALTER TABLE statement has run.
I have noticed the same thing, but I've never seen any official documentation that explains this.
I agree it seems like InnoDB "knows" which indexes were created implicitly and which were created explicitly. But I don't know where it tracks this information. InnoDB exposes much of the metadata in INFORMATION_SCHEMA tables, but it must store more information in the internal data dictionary.
This is the only documentation about the internal DD in MySQL 5.7 and earlier: https://dev.mysql.com/doc/refman/5.7/en/innodb-data-dictionary.html
The only suggestion I have is that if you need the index name to be predictable, you need to create the index explicitly, then create the foreign key constraint. Don't rely on implicit creation of indexes by foreign keys.
MySQL 8.0 has totally redesigned the data dictionary, so the behavior you observe might change yet again. https://dev.mysql.com/doc/refman/8.0/en/data-dictionary.html

Need help creating foreign keys sql error 1215

Hello my appologies for asking aid but i'm having a little issue in a database adding references
so i got a table
CREATE TABLE `kooien` (
`kooiid` INT(11) NOT NULL AUTO_INCREMENT,
`kooidlistppg` INT(11) NULL,
`quarantaine` BIT(1) NOT NULL,
`idvogelsoort` INT(11) NULL DEFAULT NULL,
`idvogelondersoort` INT(11) NULL DEFAULT NULL,
`vasteoflossekooie` BIT(1) NOT NULL,
`bezetofniet` BIT(1) NOT NULL,
`idlistsponsor` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`kooiid`),
INDEX `idvogelsoort` (`idvogelsoort`),
INDEX `idvogelondersoort` (`idvogelondersoort`),
INDEX `kooien_ibfk_4_idx` (`idlistsponsor`),
CONSTRAINT `kooien_ibfk_2` FOREIGN KEY (`idvogelsoort`) REFERENCES `vogelsoort` (`id`),
CONSTRAINT `kooien_ibfk_3` FOREIGN KEY (`idvogelondersoort`) REFERENCES `ondersoort` (`id`),
CONSTRAINT `kooien_ibfk_4` FOREIGN KEY (`idlistsponsor`) REFERENCES `personen` (`id`),
CONSTRAINT `FK_kooien_kooientoppg` FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`))
COLLATE='utf8_general_ci'
ENGINE=InnoDB
Which appears like
kooiid|kooidlistppg|quarantaine|idvogelsoort|idvogelondersoort|Vasteoflossekooien|bezetofniet|lijstlist sponsor
Now my problem is making kooidlistppg references to kooidlistppg(id)
i do so with this query
ALTER TABLE `kooien`
DROP INDEX `FK_kooien_kooientoppg`,
ADD CONSTRAINT `FK_kooien_kooientoppg` FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`);
/* SQL Fout (1215): Cannot add foreign key constraint */
ill add the create query for the table
CREATE TABLE `kooientoppg` (
`id` INT(11) NOT NULL,
`idpapegaaien` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
please know that its lack of a primary key is to convert a list to a mysql database
As explained in the MySQL manual, details of FOREIGN KEY errors with InnoDB can be checked with the following SQL command:
SHOW ENGINE INNODB STATUS
For example:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2017-01-22 23:43:46 0x7fc9fc0f1700 Error in foreign key constraint of table test/kooien:
FOREIGN KEY (`kooidlistppg`) REFERENCES `kooientoppg` (`id`))
COLLATE='utf8_general_ci'
ENGINE=InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html for correct foreign key definition.
As far as I can tell, adding an index to the referenced column would help:
ALTER TABLE kooientoppg ADD INDEX id_idx (id);

Inconsistent honoring of foreign key constraint

We have the following schema (simplified for readability):
CREATE TABLE `group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `device` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id` (`group_id`),
CONSTRAINT `device_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Our automated tests create a device referencing an existing group and then attempt to delete the group, which fails due to the default ON DELETE RESTRICT clause:
Error 1451: Cannot delete or update a parent row: a foreign key constraint fails
(`device`, CONSTRAINT `device_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `group`
(`id`))
However, around 25% of the time, the deletion of the group succeeds, despite the foreign key constraint. This leads to data inconsistency, where we have a device row referencing a group_id which does not exist.
MySQL version is 5.7.10, running the official Docker image
Tables are InnoDB
FOREIGN_KEY_CHECKS is set to 1
Any ideas why the enforcement of the foreign key constraint could be inconsistent?
This was caused by a test fixture library (https://github.com/go-testfixtures/testfixtures) which disabled foreign key checks

Attempting to add a foreign key to a table fails?

I have two tables -
CREATE TABLE `FOO` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
/*Nothing to see here*/
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=380 DEFAULT CHARSET=latin1;
CREATE TABLE `BAR` (
`ID` int(11) unsigned NOT NULL,
`UserID` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `VSK_UserID_Index` (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I am attempting create a foreign key constraint on BAR.UserID referencing users.user_id -
ALTER TABLE `FOO`.`BAR`
ADD CONSTRAINT `BAR_UserID_FKey`
FOREIGN KEY (`UserID`)
REFERENCES `FOO`.`users` (`user_id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
I keep getting this error -
Operation failed: There was an error while applying the SQL script to the database.
Error 1452: Cannot add or update a child row: a foreign key constraint fails
Both of these tables have data in them - could this be the reason why this is happening, or is there something wrong with how the tables are being created?
Is there something I need to alter on one of these tables to make this work?
The data already in one of the tables (in particular, `FOO`.`BAR`, since that is the one you're adding a constraint to) is not consistent with the data in `FOO`.`users` (`user_id`).
You must ensure that the values un the `FOO`.`BAR`.`UserID` column all exist in `FOO`.`users` (`user_id`). There may be null values or other values that do not exist in the other column.

Mysql Query Error #1005 150

I have a query but its showing error #1005 and 150 in details. Some innodb
How to get it right?
CREATE TABLE `iars` ( `id` int(10) unsigned NOT NULL auto_increment,
`SessionId` int(10) unsigned NOT NULL,
`TeacherId` int(10) unsigned NOT NULL,
`courseid` int(4) unsigned NOT NULL,
`semester` int(4) unsigned NOT NULL,
`PaperId` int(4) unsigned NOT NULL,
`groupid` int(4) unsigned NOT NULL,
`Type` varchar(4) default 1,
PRIMARY KEY (`id`),
UNIQUE KEY `Constrint_Index`
(`SessionId`,`TeacherId`,`courseid`,`semester`,`PaperId`,`groupid`),
KEY `FK_tid` (`TeacherId`),
KEY `FK_gid` (`GroupId`),
KEY `FK_pid` (`PaperId`),
CONSTRAINT `FK_gid` FOREIGN KEY (`GroupId`) REFERENCES `groups` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_pid` FOREIGN KEY (`PaperId`) REFERENCES `papers` (`p_id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ssessionid` FOREIGN KEY (`SessionId`) REFERENCES `sessions` (`id`) ON
DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_tid` FOREIGN KEY (`TeacherId`) REFERENCES `teachers` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE )
From the MySQL manual:
You cannot issue DROP TABLE for an InnoDB table that is referenced by a FOREIGN KEY constraint, unless you do SET foreign_key_checks = 0. When you drop a table, the constraints that were defined in its create statement are also dropped.
If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.
This means one of your other tables is probably referencing a column in iars which you are not re-creating. The solution, then, is to weed through all of your tables (via describe queries) and see where the reference is and either add the referenced column to your CREATE TABLE here, or remove the reference, as appropriate for your schema.
edit a very helpful gem from later on that referenced page:
SHOW ENGINE INNODB STATUS;
reveals loads of good info on the foreign-key error which just occurred. It should point you exactly where you need to look.