简体   繁体   中英

How do I delete a foreign key constraint that doesn't exist?

I ran across this problem to day in the process of trying to change my PSA_Landing.tblSubsDetails SubmissionID field (Which is currently the PK) to an Auto Increment field for some quick testing of something unrelated.

When trying to run:

ALTER TABLE `PSA_Landing`.`tblSubsDetails` CHANGE COLUMN `SubmissionID`
`SubmissionID` INT(11) NOT NULL AUTO_INCREMENT ;

I get error:

ERROR 1833: Cannot change column 'SubmissionID': used in a foreign key 
constraint 'fk_StatusSubmissionId' of table 'PSA_Landing.tblSubsStatus'

The problem being... I don't have a table PSA_Landing.tblSubsStatus... I renamed that to PSA_Landing.tblSubmissionStatus a while back. It also does not have a FK on it.

When trying to alter tblSubmissionStatus BACK to tblSubsStatus (just to see if it helps) I get an Error 1025:

 Apply changes to tblSubsStatus Error 1025: Error on rename of
'./PSA_Landing/tblSubmissionStatus' to './PSA_Landing/tblSubsStatus' (errno: 
150 - Foreign key constraint is incorrectly formed) SQL Statement: ALTER 
TABLE `PSA_Landing`.`tblSubmissionStatus`  RENAME TO `PSA_Landing`.`tblSubsStatus`

So I then ran:

SHOW ENGINE innodb STATUS

And find this:

 ------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-01-24 03:15:32 7fc4410fb700 Error in foreign key constraint of table PSA_Landing/tblSubsStatus:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT "fk_StatusSubmissionId" FOREIGN KEY ("fk_SubmissionId") REFERENCES "tblSubsDetails" ("SubmissionID") ON DELETE NO ACTION ON UPDATE NO ACTION

Finally this lead me to try:

alter table PSA_Landing.tblSubsStatus
drop foreign key fk_StatusSubmissionID

Which expectedly gives:

alter table PSA_Landing.tblSubsStatus  drop foreign key fk_StatusSubmissionID      
Error Code: 1146. Table 'PSA_Landing.tblSubsStatus' doesn't exist

And out of desperation I tried this:

alter table PSA_Landing.tblSubmissionStatus
drop foreign key fk_StatusSubmissionID

Which yields:

Error Code: 1091. Can't DROP 'fk_StatusSubmissionID'; check that column/key exists  

So in short...

HOW do I delete constraint fk_StatusSubmissionId from table PSA_Landing.tblSubsStatus when that table does not exist??

SET @@FOREIGN_KEY_CHECKS = 0; causes InnoDB to disable referential integrity checking on DML and to permit some invalid DDL operations related to foreign keys, such as creating a table with a foreign key constraint referencing a table that doesn't yet exist, or dropping a table referenced by a foreign key, while still disallowing blatantly invalid requests, like REFERENCES referring to a column with a mismatched data type, or malformed foreign key definitions.

This statement operates at the session level, so it doesn't disable foreign key constraint enforcement for the entire server. It is normally used only during the process of restoring from backups (the statement is injected into dump files by mysqldump and periodically executed during the reload operation) and is operationally necessary since it's not always possible to resolve circular dependencies in foreign keys (and mysqldump doesn't see a need to -- tables are dumped in lexical order).

In this case, the solution was to recreate and then drop the phantom referencing table, while FOREIGN_KEY_CHECKS was disabled.

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_foreign_key_checks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM