I got this error when i was trying to alter my table.
Error Code: 1833. Cannot change column 'person_id': used in a foreign key constraint 'fk_fav_food_person_id' of table 'table.favorite_food'
Here is my CREATE TABLE STATEMENT Which ran successfully.
CREATE TABLE favorite_food(
person_id SMALLINT UNSIGNED,
food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY(person_id,food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id)
REFERENCES person (person_id)
);
Then i tried to execute this statement and i got the above error.
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
The type and definition of foreign key field and reference must be equal.
This means your foreign key disallows changing the type of your field.
One solution would be this:
Now you can change you person_id
recreate foreign key
EDIT:
Added locks above, thanks to comments
I’ve added a write lock above
All writing queries in any other session than your own (
INSERT, UPDATE, DELETE) will wait till timeout orUNLOCK TABLES; is executedhttp://dev.mysql.com/doc/refman/5.5/en/lock-tables.html
EDIT 2: OP asked for a more detailed explanation of the line “The type and definition of foreign key field and reference must be equal. This means your foreign key disallows changing the type of your field.”
From MySQL 5.5 Reference Manual: FOREIGN KEY Constraints