When you want to change the length of Foreign Key. Error #1832

I tried to change the foreign key of product_category_id which was presented in products_products table. The following step help you to change the length of foreign key. Before moving to step, you have to focus on error. When we tried to change the length it provides the error.

#1832 - Cannot change column 'product_category_id': used in a foreign key constraint 'products_products_product_category_id_803e9c08_fk_products_'

This error message contains of constraint name which is used in further step. Now, we drop the Foreign Key Constraint. This can be done through below step.

ALTER TABLE products_products DROP FOREIGN KEY constraint_name;

Now, change the product_category_id column type to CHAR(36) or BINARY(16), depending on your UUID storage preference:

ALTER TABLE products_products MODIFY COLUMN product_category_id CHAR(36);

After modifying the column, recreate the foreign key constraint:

ALTER TABLE products_products
ADD CONSTRAINT constraint_name
FOREIGN KEY (product_category_id) REFERENCES products_productcategory(id);

By this step, we can change the length of Foreign Key. Hope, this solution help you to resolve your problem. Thank you and have a nice day.

Leave a Comment

Your email address will not be published. Required fields are marked *