I have two tables public_a.budget_items and public_b.budget_items with identical schemes and I'm trying to copy missing records from one of them to the other using this query:
INSERT INTO public_b.budget_items
SELECT a.*
FROM public_a.budget_items a
WHERE a.id NOT IN (SELECT b.id
FROM public_b.budget_items b
WHERE b.id = a.id
OR ( b.budget_id = a.budget_id
AND b.NAME = a.NAME
AND b.type = a.type
AND b.asset = a.asset ))
but I'm getting this error:
ERROR: duplicate key value violates unique constraint "uc_budget_items_parent_null" SQL state: 23505 Detail: Key (budget_id, name, type, asset)=(3486, Octopus, 6, T) already exists
The constraint uc_budget_items_parent_null is defined as follows:
CREATE UNIQUE INDEX uc_budget_items_parent_null ON budget_items USING btree (
budget_id, name, type, asset);
I thought that the condition (b.budget_id=a.budget_id and b.name=a.name and b.type = a.type and b.asset = a.asset) should help me avoid this problem, but it doesn't seem to work.
Clearly I'm missing something here. Any ideas?
where a.id not in (select id from public_b.budget_items)work?uc_budget_items_parent_null. I have to take those constraints into account in the query.