Welcome Guest, Not a member yet? Register   Sign In
migration: change field type in FK
#1

(This post was last modified: 02-21-2024, 07:20 AM by sjender.)

I want to change a bunch of fieldtypes (from smallint to mediumint).
But most of them are foreign keys, so updating them results in "Cannot change column 'X': used in a foreign key constraint 'Y'...
Is there a way of doing this with a migration which doesn't involve deleting the key and adding it again?
Something like:
Code:
SET FOREIGN_KEY_CHECKS=0; ???
Reply
#2

See https://codeigniter.com/user_guide/dbmgm...reign-keys
Reply
#3

Disable foreign key checks temporarily:
sql
Copy code
SET FOREIGN_KEY_CHECKS=0;
Alter the column types as needed:
sql
Copy code
ALTER TABLE your_table MODIFY your_column MEDIUMINT;
Enable foreign key checks again:
sql
Copy code
SET FOREIGN_KEY_CHECKS=1;
By temporarily disabling foreign key checks, you can modify the column types without encountering errors related to foreign key constraints. After the alterations are complete, remember to re-enable foreign key checks to maintain data integrity.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB