Cannot drop index 'idx': needed in a foreign key constraint
So I had this table with its migration in the database:
Then modified it to the following: ( dropped id and made the userId the primaryKey )
When I'm trying to apply the migration, I get the error: "Cannot drop index 'user_id_idx': needed in a foreign key constraint" How can this type of thing be safely done? And is it a drizzle problem?
1 Reply
This was the SQL code that was throwing the error:
DROP INDEX
user_id_idx
ON user_profile
;--> statement-breakpoint
ALTER TABLE user_profile
ADD PRIMARY KEY (user_id
);--> statement-breakpoint
ALTER TABLE user_profile
DROP COLUMN id
;--> statement-breakpoint
Changing the order of the statements makes the script run successfully
ALTER TABLE user_profile
DROP COLUMN id
;--> statement-breakpoint
ALTER TABLE user_profile
ADD PRIMARY KEY (user_id
);--> statement-breakpoint
DROP INDEX user_id_idx
ON user_profile
;--> statement-breakpoint