Postgres CTE issues

From: Shekar Tippur <ctippur(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Postgres CTE issues
Date: 2015-05-26 15:45:32
Message-ID: CAHmfATopobYPPVb5HRS-wZfuKkXLcz7gmbcx6bFpsKs1KE92-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

I am new to postgres. I have a scenario where I need a trigger that inserts
values into n tables but dome of those tables are related.

I dug up some documents and I stumbled across CTE or teh with statement.

This is what I am trying:

WITH x AS

(INSERT INTO industry (name,abbr,description,cr_date,last_upd)

VALUES ('df','','',now(),now()) returning id) insert into sector
(name,description,cr_date,last_upd,industry_id) select
's1','',now(),now(),id from x;

I get a error:

ERROR: insert or update on table "sector" violates foreign key constraint
"sector_id_fkey"

DETAIL: Key (id)=(394) is not present in table "industry".

If I execute the insert individually, I am able to insert a record. Wonder
what I am doing wrong.

I have been stuck with this issue for over 24 hours. Appreciate any help.

- Shekar

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2015-05-26 16:00:09 Re: Postgres CTE issues
Previous Message David G. Johnston 2015-05-21 23:54:42 Re: Group By with condition