Re: Unit tests and foreign key constraints

From: Andy Chambers <achambers(dot)home(at)gmail(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>, Andy Chambers <achambers(dot)home(at)googlemail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Unit tests and foreign key constraints
Date: 2015-05-21 21:41:21
Message-ID: CAHnaAcyU2xuaq2BZ-es7x6=CkOca_ezeha_DLm0L5BWZdsUr0w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, May 21, 2015 at 1:34 PM, Martijn van Oosterhout <kleptog(at)svana(dot)org>
wrote:

> On Thu, May 21, 2015 at 12:39:01PM -0700, Andy Chambers wrote:
> > Hey All,
> >
> > I've started trying to use foreign key constraints in my schema but it
> > seems to make it more difficult to write unit tests that touch the
> database
> > because each test now requires more setup data to satisfy the foreign key
> > constraint. (I know some say your unit tests shouldn't touch the DB but
> the
> > more full stack tests I have, the better I sleep at night :-))
> >
> > I wondered if anyone else has run into this problem and found a good
> > strategy to mitigate it. I thought I might be able to make these
> > constraints deferred during a test run since I have automatic rollback
> > after each test but even after "set constraints all deferred", I still
> got
> > a foreign key violation during my test run if the test tries to insert
> data
> > with a non-existent foreign key.
>
> Foreign keys aren't deferrable by default, you have to create them that
> way...
>

Ah that's what I was missing. Thanks!

--
Andy

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Karsten Hilbert 2015-05-21 21:42:32 Re: date with month and year
Previous Message Brian Dunavant 2015-05-21 21:35:27 Re: date with month and year