pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: dabrown+pg@goscoutgo.com
Subject: BUG #15422: add column statement with if not exists creates a new foreign key even if the column already exists
Date: Mon, 08 Oct 2018 04:17:51 +0000
Message-ID: <15422-1f1ee97a6a8c57df@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      15422
Logged by:          Dale B
Email address:      dabrown+pg@goscoutgo.com
PostgreSQL version: 10.5
Operating system:   Docker (postgres:latest) on Ubuntu 14.04
Description:        

# Bug description

Foreign key is still created for an `alter table ... add column if not
exists ... references ... ;` statement even if the column exists. I would
expect no schema change to occur if the column already exists, but instead a
new foreign key constraint is added every time the query is run.

# Reproduction steps

On a fresh postgres 10.5 install:
```
create table if not exists test (id int primary key);
create table if not exists test2 (test_id int);
\d+ test;
alter table test2 add column if not exists test_id int references
test(id);
\d+ test;
alter table test2 add column if not exists test_id int references
test(id);
\d+ test;
```

This gives me the following output:
```
CREATE TABLE
CREATE TABLE
                         Table "public.test"
 Column |  Type   | Modifiers | Storage | Stats target | Description 
--------+---------+-----------+---------+--------------+-------------
 id     | integer | not null  | plain   |              | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)

psql:test.sql:4: NOTICE:  column "test_id" of relation "test2" already
exists, skipping
ALTER TABLE
                         Table "public.test"
 Column |  Type   | Modifiers | Storage | Stats target | Description 
--------+---------+-----------+---------+--------------+-------------
 id     | integer | not null  | plain   |              | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "test2" CONSTRAINT "test2_test_id_fkey" FOREIGN KEY (test_id)
REFERENCES test(id)

psql:test.sql:6: NOTICE:  column "test_id" of relation "test2" already
exists, skipping
ALTER TABLE
                         Table "public.test"
 Column |  Type   | Modifiers | Storage | Stats target | Description 
--------+---------+-----------+---------+--------------+-------------
 id     | integer | not null  | plain   |              | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "test2" CONSTRAINT "test2_test_id_fkey" FOREIGN KEY (test_id)
REFERENCES test(id)
    TABLE "test2" CONSTRAINT "test2_test_id_fkey1" FOREIGN KEY (test_id)
REFERENCES test(id)

```



Message-ID: <15422-1f1ee97a6a8c57df@postgresql.org>
Permalink:  ../15422-1f1ee97a6a8c57df@postgresql.org/
Also on:    postgresql.org/message-id/15422-1f1ee97a6a8c57df@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, dabrown+pg@goscoutgo.com
  Subject: Re: BUG #15422: add column statement with if not exists creates a new foreign key even if the column already exists
  In-Reply-To: <15422-1f1ee97a6a8c57df@postgresql.org>
  References: <15422-1f1ee97a6a8c57df@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox