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?
1 Reply
Boby
Boby2mo ago
You can try out setting some default values for the new onUpdate column .default(null)
It might work, I also ran into the same issue, though my db had a very few data so I had deleted the migrations folder then I again migrated using the updated query

Did you find this page helpful?