Question

I'm trying to create this table:

CREATE TABLE `TSO` (
    `TSO_ID` INT UNSIGNED NOT NULL
)

and I'm getting SQL error (1215): Cannot add foreign key constraint, what could it be? If I change the table name it works.

Thanks

Was it helpful?

Solution 2

Ok, I managed to fix it, here's what happened.

I had a table "TSO" and a table "SO" which referenced to TSO, I somehow managed to delete TSO without getting any constraint error (which I believe I should have, even though "SO" was empty).

After that any time I tried to insert the table with the name "TSO" I was getting that error, because the table "SO" still had reference to the now deleted table with the same name. After I deleted "SO" too I was finally able to insert TSO again.

OTHER TIPS

The possibility is:

There was a same-named table dropped. As per mysql documentation:

http://dev.mysql.com/doc/refman/5.0/en/create-table-foreign-keys.html

"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 correct column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns Error 1005 and refers to Error 150 in the error message, which means that a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails due to Error 150, this means that a foreign key definition would be incorrectly formed for the altered table."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top