Constraint does not exist when running migration

This is happening because one of my tables was looking like this:
export const profiles = pgTable('profiles', {
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' });
... rest of the columns
});
export const profiles = pgTable('profiles', {
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' });
... rest of the columns
});
Then on a new migration I've added the onUpdate
export const profiles = pgTable('profiles', {
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade', onUpdate: 'cascade' });
... rest of the columns
});
export const profiles = pgTable('profiles', {
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade', onUpdate: 'cascade' });
... rest of the columns
});
This change made the migration file to have to alter table to change the constraint
DO $$ BEGIN
ALTER TABLE "profiles" ADD CONSTRAINT "profiles_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE cascade;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
DO $$ BEGIN
ALTER TABLE "profiles" ADD CONSTRAINT "profiles_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE cascade;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
And now when I run this specific migration I'm facing the error: "Constraint 'profiles_user_id_users_id_fk' of relation "profiles" does not exist" Any tips how to solve this?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?