Problems with MySQL using InnoDB and dropping an unused, foreign key. The foreign key references another table's id. However, I don't need this field.
I've tried removing the fk index, which doesn't work - says it's needed in a foreign key contraint. And removing the field, which gives me an error:
1025 - Error on rename of './axis/#sql-ad8_1531' to './axis/Schedule' (errno: 150)
The table is currently empty. There are no tables referencing this field. Any ideas on how to get rid of this? Other than creating a new table?
If I'm reading the below error correctly, I can't drop the column since the fk index is declared. And I can't drop the index, because the column exists. Chicken & Egg??
LATEST FOREIGN KEY ERROR
111004 17:05:40 Error in foreign key constraint of table axis/Schedule:
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_Schedule_Grp" FOREIGN KEY ("idGrp") REFERENCES "Grp" ("idGrp") ON DELETE NO ACTION ON UPDATE NO ACTION
InnoDB: Renaming table axis. to axis.Schedule failed!
You need to use
ALTER TABLE table_name DROP FOREIGN KEY constraint_name
Here constraint_name is the name of the constraint rather than the index. If you do not know what this is, you can find out by issuing a SHOW CREATE TABLE. It is the identifier that appears after the word CONSTRAINT.
Edit: From your addition to the question, it looks like you need to issue
ALTER TABLE table_name DROP FOREIGN KEY fk_Schedule_Grp
Related
I am trying to rename an empty table with its old name.
I had created constraints in the past (innodb), but that table doesn't exist anymore and if I try to reuse its name (no matter if I create a new table or try to rename another table), I get the following error:
errno: 150 "Foreign key constraint is incorrectly formed"
LATEST FOREIGN KEY ERROR
------------------------
2021-07-16 14:50:24 0x2508 Error in foreign key constraint of table database/publisheremails:
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 `publisheremails_ibfk_1` FOREIGN KEY (`agencyid`) REFERENCES `agencies` (`id`) ON UPDATE CASCADE
I tried to drop the constraint with this code:
ALTER TABLE old_publisheremails
DROP FOREIGN KEY publisheremails_ibfk_1;
I get the following error:
#1091 - Can't DROP FOREIGN KEY publisheremails_ibfk_1; check that it exists
I also tried to drop it with the old table name, but I get this error:
#1146 - Table 'database.publisheremails' doesn't exist
Same goes if I try to drop it from 'agencies'...
Check which foreign keys the table actually has and which name it has with
SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = '<database>' AND
REFERENCED_TABLE_NAME = 'old_publisheremails';
And you can't drop the publisheremails_ibfk_1 because it doesn't exist till now
So check the agencyid column if it has the same data type as agencies (id`) and change it
I use MySQL with InnoDB engine. I double-checked type of columns. But always have:
Error Code: 1215. Cannot add foreign key constraint
I tried:
ALTER TABLE `mail`.`boxes`
ADD CONSTRAINT FK_id
FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
and
ALTER TABLE `mail`.`boxes`
ADD FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
Nothing works(((
Please, help, what I am doing wrong (except choosing MySQL :-) )?
If table contains data then you are not able to add foreign key you drop table object and recreate
use below reference for the same
Basics of Foreign Keys in MySQL?
To check what exactly the problem is, use:
SHOW ENGINE INNODB STATUS\G
There is section "last foreign key error". Look at: http://dev.mysql.com/doc/refman/5.0/en/innodb-monitors.html
My guess is that data type od mail.boxes (id) and mail.users (id) is not the same. (E.g. smallint in one table and integer in second one).
Data in table on which you're trying to create FK could possibly also be problem (are your mailbox ids the same as id of existing users?)
I'm trying to rename a primary key in an InnoDB table and I kept getting an Errno 150. SHOW INNODB STATUS shows:
LATEST FOREIGN KEY ERROR
130711 18:22:53 Error in foreign key constraint of table xx/client_location_business_load:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "business_load_business_load_name_key" FOREIGN KEY ("name_id") REFERENCES "client_businesstype_load_name" ("name_id") ON DELETE CASCADE ON UPDATE CASCADE
The table client_location_business_load does not even exist! It was renamed. The constraint named business_load_business_load_name_key does not exist either, it was dropped and it does not appear in information_schema.key_column_usage.
Does anyone have a clue about what's going on here?
You have to drop the database and recreate it in the same collation. There does not seem to be an alternative.
I am trying to set up a foreign key in Mysql workbench. I used the same name for the foreign key as the primary key of the table I am trying to set a relationship with. I already have one relation set up this way in another table, but When I try and apply the alterations to this table, the script gives me an error:
Error 1005: Can't create table 'X.#sql-718_a' (errno: 121)
SQL Statement:
ALTER TABLE `X`.`X_use`
ADD CONSTRAINT `XyzID`
FOREIGN KEY (`XyzID` ) REFERENCES `X`.`Xyz` (`XyzID` )
ON DELETE NO ACTION O
N UPDATE NO ACTION ,
ADD INDEX `XyzID` (`XyzID` ASC) ,
However, if I change the foreign key name to "AbcID" I have no problem setting up the foreign key relation. Why is that and why can't I have the primary key name from one table be the same for the foreign key for this table? I have set up relations like that previously but for this table I cannot.
Constraint names have to be unique within the database.
That (errno: 121) in the error message means that MySQL encountered a duplicate key exception. The usual cause of this is that there is already a constraint of the same name in the database.
This "unique name" requirement is one reason why the normative pattern is to include the table name when constructing the name of the foreign key constraint. e.g. FK_table_cols e.g. FK_X_use_XyzID.
Why must the constraint name be unique within the database? That's a question for the dbms designers.
But consider this: when the database encounters a constraint violation, it throws an error that contains the name of the constraint. When that constraint name references only one constraint in the database, that makes locating the problem a bit easier.
You can not use same constrain name through out the database as described in ACID properties of DB.
So I am trying to add a primary key to one of the tables in my database. Right now it has a primary key like this:
PRIMARY KEY (user_id, round_number)
Where user_id is a foreign key.
I am trying to change it to this:
PRIMARY KEY (user_id, round_number, created_at)
I am doing this in phpmyadmin by clicking on the primary key icon in the table structure view.
This is the error I get:
#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)
It is a MySQL database with InnoDB table engine.
There is probably another table with a foreign key referencing the primary key you are trying to change.
To find out which table caused the error you can run SHOW ENGINE INNODB STATUS and then look at the LATEST FOREIGN KEY ERROR section.
As was said you need to remove the FKs before. On Mysql do it like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
For those who are getting to this question via google... this error can also happen if you try to rename a field that is acting as a foreign key.
To bypass this in PHPMyAdmin or with MySQL, first remove the foreign key constraint before renaming the attribute.
(For PHPMyAdmin users: To remove FK constrains in PHPMyAdmin, select the attribute then click "relation view" next to "print view" in the toolbar below the table structure)
If you are trying to delete a column which is a FOREIGN KEY, you must find the correct name which is not the column name. Eg: If I am trying to delete the server field in the Alarms table which is a foreign key to the servers table.
SHOW CREATE TABLE alarm;
Look for the CONSTRAINT `server_id_refs_id_34554433` FORIEGN KEY (`server_id`) REFERENCES `server` (`id`) line.
ALTER TABLE `alarm` DROP FOREIGN KEY `server_id_refs_id_34554433`;
ALTER TABLE `alarm` DROP `server_id`
This will delete the foreign key server from the Alarms table.
I had this problem, it is for foreign-key
Click on the Relation View (like the image below) then find name of the field you are going to remove it, and under the Foreign key constraint (INNODB) column, just put the select to nothing! Means no foreign-key
Hope that works!
If you are adding a foreign key and faced this error, it could be the value in the child table is not present in the parent table.
Let's say for the column to which the foreign key has to be added has all values set to 0 and the value is not available in the table you are referencing it.
You can set some value which is present in the parent table and then adding foreign key worked for me.