Skip to content

Instantly share code, notes, and snippets.

@birgersp
Last active March 20, 2024 06:56
Show Gist options
  • Save birgersp/f902625ed25a4394406aed5f51847976 to your computer and use it in GitHub Desktop.
Save birgersp/f902625ed25a4394406aed5f51847976 to your computer and use it in GitHub Desktop.
Add (change) foreign key constraint to an existing column in PostgreSQL
-- https://dba.stackexchange.com/questions/323851/add-foreign-key-constraint-to-existing-column-while-auto-generating-the-key-nam
alter table "Message"
-- drop the old constraint
drop constraint "Message_sender_fkey",
-- (re)add constraint but this time with cascading
add foreign key ("sender") references "Sender"("id") on delete cascade;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment