7

I renamed a table, and apparently a foreignkey in it doesn't get changed, and causing problems, Although when I \d error I get:
"error_test_id_fkey" FOREIGN KEY (test_id) REFERENCES test(id) ON DELETE CASCADE

Which the only relation to error name, is the name (weird)

Anyway - I have no problem deleting the foreignkey and recreating it, but I can't figure out how to do it

  • It seems worth mentioning that technically the name of the foreign key is in no way related to the names of the objects it refers from/to. It is a quite useful convention though to give the foreign keys names that let the human user derive their purpose. Therefore in such a case I recommend that you also rename the foreign key. Luckily an answer for that is already present :) – SebastianH May 15 at 8:59
15

If you are using PostgreSQL 9.2 or newer, you can use RENAME CONSTRAINT:

ALTER TABLE name RENAME CONSTRAINT "error_test_id_fkey" TO "the_new_name_fkey";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.