Re: Deleting conflicting rows when creating a foreign key

Поиск
Список
Период
Сортировка
Искать
От
Richard Huxton
Тема
Re: Deleting conflicting rows when creating a foreign key
Дата
Msg-id
49919352.3000603@archonet.com
Ответ на
Список
Дерево обсуждения
Deleting conflicting rows when creating a foreign key Igor Katson <descentspb@gmail.com>
Re: Deleting conflicting rows when creating a foreign key Richard Huxton <dev@archonet.com>
Re: Deleting conflicting rows when creating a foreign key Craig Ringer <craig@postnewspapers.com.au>
Re: Deleting conflicting rows when creating a foreign key Igor Katson <descentspb@gmail.com>
Igor Katson wrote:
> I am doing an ALTER TABLE to create a foreign key, however with some
> rows i get:
> 
> insert or update on table "name" violates foreign key constraint
> "name_fkey"
> 
> How can I just drop the conflicting rows while doing that?

You can't automatically. You can do something like:

BEGIN;
SELECT * FROM table1 WHERE col1 NOT IN (SELECT id from table2);
-- check results are as you'd expect
DELETE FROM table1 WHERE col1 NOT IN (SELECT id from table2);
ALTER TABLE table1 ADD CONSTRAINT ... FOREIGN KEY ...
COMMIT;

-- 
  Richard Huxton
  Archonet Ltd
В списке pgsql-general по дате отправления
От: Richard Huxton
Дата:
Сообщение: Re: Slow database creation
От: SHARMILA JOTHIRAJAH
Дата:
FAQ