Re: Postgres CTE issues

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: Postgres CTE issues
Дата
Msg-id CAKFQuwbMEZFiNKiaCKUvvoS854bkE3Dqa288vcGYTg3DtK09Og@mail.gmail.com
обсуждение исходный текст
Ответ на Postgres CTE issues  (Shekar Tippur <ctippur@gmail.com>)
Список pgsql-sql
On Tue, May 26, 2015 at 8:45 AM, Shekar Tippur <ctippur@gmail.com> wrote:

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.


It is not possible to accomplish your goal using a CTE.  From the point of view of both tables the data they can see is what was present before the statement began.

The more usual way to accomplish this is the write a pl/pgsql function with two statements and passing the ID between them using an intermediate variable.

​David J.

В списке pgsql-sql по дате отправления:

Предыдущее
От: Shekar Tippur
Дата:
Сообщение: Postgres CTE issues
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Fwd: Postgres CTE issues