Re: BUG #17247: How to avoid crating multiple Foreign keys on same column on same table.

From: Juan José Santamaría Flecha <juanjo(dot)santamaria(at)gmail(dot)com>
To: nbipin29(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17247: How to avoid crating multiple Foreign keys on same column on same table.
Date: 2021-10-26 08:31:25
Message-ID: CAC+AXB1w_XQSDn9R6pskD8dtta7kCC-KFOy8Mc3sCeqUK1EJdw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Oct 26, 2021 at 9:07 AM PG Bug reporting form <
noreply(at)postgresql(dot)org> wrote:

>
> Issue: Postgres engine allows multiple FK on same columns for a same
> table.This cause duplication and unwanted use of space . If we accidentally
> run a script multiple times it creates the FK mulitple times.
> Ran the following statement 3 times : to add a foreign key
> ALTER TABLE admin.emp
> ADD FOREIGN KEY (deptno)
> REFERENCES admin.dept (deptno)
> ON DELETE NO ACTION;
>
> I see 3 its created thrice . In Oracle this statement should fail. Is there
> any way this restriction can be applied on PG.
> admin emp_deptno_fkey emp deptno admin dept deptno
> admin emp_deptno_fkey1 emp deptno admin dept deptno
> admin emp_deptno_fkey2 emp deptno admin dept deptno
>
> Please advice.
>
> Using a named constraint can prevent this:

ALTER TABLE admin.emp ADD CONSTRAINT emp_deptno_fkey FOREIGN KEY (deptno)
REFERENCES admin.dept (deptno) ON DELETE NO ACTION;

Regards,

Juan José Santamaría Flecha

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Alexander Korotkov 2021-10-26 09:34:32 Re: BUG #17229: Segmentation Fault after upgrading to version 13
Previous Message David Rowley 2021-10-26 07:50:56 Re: BUG #17213: Wrong result from a query involving Merge Semi Join and Memoize