PostgreSQL Unit Test – Deferrable Foreign Key with PgTap

ddlpostgresqlunit test

I am using PgTap to unit test a Postgres (v9.4) database structure in a continuous migration process. It works great.

Here is my question: « Is there a way to control that a foreign key constraint is deferrable (initially immediate) ? »

I do already check foreign keys but I want to ensure some of them are deferrable.

Thank you for your help.

Best Answer

Ok, it appears there are no methods to test such thing with PgTap for now so I ended up with

SELECT results_eq(
    $$select condeferrable, condeferred from pg_catalog.pg_constraint where conname = 'default_article_id_fk'$$,
    $$values ('t'::bool, 'f'::bool)$$,
    '"default_article_id_fk" is deferrable initially immediate.'
);

That does the job.