RE: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"

From: Shatamjeev Dewan <sdewan(at)nbsps(dot)com>
To: Michael Lewis <mlewis(at)entrata(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: RE: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"
Date: 2019-11-18 18:58:24
Message-ID: F2F23E564EB92A4C80228EB86713BA130BF9B0@TO1RODC.nbspaymentsolutions.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks a lot Michael. Get well soon.

From: Michael Lewis <mlewis(at)entrata(dot)com>
Sent: November-18-19 12:57 PM
To: Shatamjeev Dewan <sdewan(at)nbsps(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"

On Mon, Nov 18, 2019 at 10:10 AM Shatamjeev Dewan <sdewan(at)nbsps(dot)com<mailto:sdewan(at)nbsps(dot)com>> wrote:
I am trying to create a foreign key constraint on a table : audit_param in postgres 12 which references partitioned table audit_p. is there anyway to get rid of this error.

ERROR: there is no unique constraint matching given keys for referenced table "audit_p"

As far as I understand, what you want is not possible. You cannot partition on a timestamp and then foreign key to an object_id natively. You can get around this with triggers- https://www.depesz.com/2018/10/31/foreign-key-to-partitioned-table-part-2/ - but that would not be recommended.

Admittedly, I have a pretty nasty head cold so it may be that someone chimes in with much better insight on your design.

By the way, there are strong recommendations for using timestamp WITH TIMEZONE in nearly all cases. I just wanted to mention since timestamp without timezone is rarely the best choice.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dave Hughes 2019-11-18 19:56:20 pgaudit log directory
Previous Message Adrian Klaver 2019-11-18 17:58:51 Re: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"