pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: John McKown <john.archie.mckown@gmail.com>
To: PostgreSQL General <pgsql-general@postgresql.org>
Subject: Re: Enforce primary key on every table during dev?
Date: Wed, 28 Feb 2018 05:57:56 -0800
Message-ID: <e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com> (raw)
In-Reply-To: <CAAJSdjimuO1SaxzCMxzYOp7ao9jPrdR5Asqun9RuOPY0FnajkQ@mail.gmail.com>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com>
	<CAAJSdjimuO1SaxzCMxzYOp7ao9jPrdR5Asqun9RuOPY0FnajkQ@mail.gmail.com>

On 02/28/2018 05:52 AM, John McKown wrote:
> On Wed, Feb 28, 2018 at 7:34 AM, Jeremy Finzel <finzelj@gmail.com 
> <mailto:finzelj@gmail.com>>wrote:
> 
>     We want to enforce a policy, partly just to protect those who might
>     forget, for every table in a particular schema to have a primary
>     key.  This can't be done with event triggers as far as I can see,
>     because it is quite legitimate to do:
> 
>     BEGIN;
>     CREATE TABLE foo (id int);
>     ALTER TABLE foo ADD PRIMARY KEY (id);
>     COMMIT;
> 
>     It would be nice to have some kind of "deferrable event trigger" or
>     some way to enforce that no transaction commits which added a table
>     without a primary key.
> 
>     Any ideas?
> 
>     Thanks,
>     Jeremy
> 
> 
> 
> ​What stops somebody from doing:
> 
> CREATE TABLE foo (filler text primary key default null, realcol1 int, 
> realcol2 text);
> 
> And then just never bother to ever insert anything into the column 
> FILLER? It fulfills your stated requirement​ of every table having a 

Then you would get this:

test=# CREATE TABLE foo (filler text primary key default null, realcol1 
int, realcol2 text);
CREATE TABLE
test=# insert into  foo (realcol1, realcol2) values (1, 'test');
ERROR:  null value in column "filler" violates not-null constraint
DETAIL:  Failing row contains (null, 1, test).

> primary key. Of course, you could amend the policy to say a "non-NULL 
> primary key".
> 
> 
> 
> -- 
> I have a theory that it's impossible to prove anything, but I can't 
> prove it.
> 
> Maranatha! <><
> John McKown


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (59+ messages)  latest in thread

Message-ID: <e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com>
Permalink:  ../e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com/
Also on:    postgresql.org/message-id/e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com

 · 

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-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, john.archie.mckown@gmail.com
  Subject: Re: Enforce primary key on every table during dev?
  In-Reply-To: <e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com>
  References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com> <CAAJSdjimuO1SaxzCMxzYOp7ao9jPrdR5Asqun9RuOPY0FnajkQ@mail.gmail.com> <e418e221-f09f-626e-28b6-1652ca19c3de@aklaver.com>

* 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