jeff
jeff

Reputation: 596

Replacing table and renaming primary key - Postgres

To preface, I am trying to replace an entire table with a new table with same columns, but with updated values.

I have the following SQL code:

BEGIN;
ALTER TABLE "original" RENAME TO "original_old";
ALTER TABLE "original_new" RENAME TO "original";
ALTER TABLE "original" RENAME CONSTRAINT "temp_original_id" to "original_id";
DROP TABLE "original_old";
COMMIT;

Output:

ERROR:  constraint "temp_original_id" for table "original" does not exist

However, if I do the following before the last ALTER statement:

SELECT * from original;

I see temp_original_id present in the table.

I can't seem to find any other sources that lead me to updating primary key (at least that worked)

The table I am replacing also has dependencies with other tables.. So I was wondering if this would be a viable solution to even begin with

Upvotes: 1

Views: 522

Answers (1)

shantr
shantr

Reputation: 804

Did you mean ALTER TABLE "original" RENAME COLUMN "temp_original_id" to "original_id"; ?

Upvotes: 2

Related Questions