0

I was trying to duplicate data from an existing table to an empty table in different schema (all in the same database) and got an error. (PostgreSQL 16):

This is for specific exercise, so I have to copy using DDL & DML commands. I have a better way, but I need this to work.

This is the error:

ERROR: Key (name)=(Baker) already exists.duplicate key value violates unique constraint "job_pkey"

ERROR: duplicate key value violates unique constraint "job_pkey"

SQL state: 23505

Detail: Key (name)=(Baker) already exists.

The query I was tried to write look like this:

INSERT INTO table_copy.job_sample (name, description, salary) SELECT name, description, salary FROM table.job; 
2
  • 2
    either the destination table is not empty (it already contains an entry for "Baker"), the two tables don't have the same constraints (duplicate names in the source table are ok) or you are inserting in the same table that you are reading. Commented Oct 30, 2024 at 18:19
  • 1
    Use an ON CONFLICT handler to deal with duplicates or clean the data before it even hits the database. Commented Oct 30, 2024 at 18:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.