pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Shatamjeev Dewan <sdewan@nbsps.com>
To: Michael Lewis <mlewis@entrata.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"
Date: Mon, 18 Nov 2019 09:58:51 -0800
Message-ID: <f9fc06d7-c8c1-31ef-4b8f-107f5385a611@aklaver.com> (raw)
In-Reply-To: <F2F23E564EB92A4C80228EB86713BA130BF984@TO1RODC.nbspaymentsolutions.com>
References: <F2F23E564EB92A4C80228EB86713BA130BF984@TO1RODC.nbspaymentsolutions.com>

On 11/18/19 9:10 AM, Shatamjeev Dewan wrote:
> Hi Michael,
> 
> 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"*
> 
>                                        ^
> 
> sd_tems_partition_test=# *CREATE TABLE audit_logging.audit_p*(
> 
> sd_tems_partition_test(#     id BIGINT   NOT NULL,
> 
> sd_tems_partition_test(#     event_id BIGINT  NOT NULL,
> 
> sd_tems_partition_test(#     caused_by_user_id BIGINT  NOT NULL,
> 
>          PARTITION BY RANGE(create_dtt);
> 
>          sd_tems_partition_test(#     -- additional user information (to 
> be defined by the application)
> 
> sd_tems_partition_test(#     adtl_user_info BIGINT,
> 
> sd_tems_partition_test(#     create_dtt TIMESTAMP DEFAULT now()  NOT NULL,
> 
> sd_tems_partition_test(#     CONSTRAINT audit_PK1 PRIMARY KEY 
> (id,create_dtt))
> 
> sd_tems_partition_test-# PARTITION BY RANGE(create_dtt);
> 
> CREATE TABLE
> 
> sd_tems_partition_test=# \d audit_logging.audit_p
> 
>                      Partitioned table "audit_logging.audit_p"
> 
>        Column       |            Type             | Collation | Nullable 
> | Default
> 
> -------------------+-----------------------------+-----------+----------+---------
> 
> id                | bigint                      |           | not null |
> 
> event_id          | bigint                      |           | not null |
> 
> caused_by_user_id | bigint                      |           | not null |
> 
> adtl_user_info    | bigint                      |           |          |
> 
> create_dtt        | timestamp without time zone |           | not null | 
> now()
> 
> Partition key: RANGE (create_dtt)
> 
> Indexes:
> 
>      "audit_pk1" PRIMARY KEY, btree (id, create_dtt)
> 
> Number of partitions: 0
> 
> sd_tems_partition_test*=# CREATE TABLE audit_logging.audit_param (*
> 
> *sd_tems_partition_test(#     audit_id BIGINT  NOT NULL,*
> 
> sd_tems_partition_test(#     param_position SMALLINT  NOT NULL,
> 
> sd_tems_partition_test(#     value CHARACTER VARYING(4096)  NOT NULL,
> 
> sd_tems_partition_test(#     CONSTRAINT audit_param_PK PRIMARY KEY 
> (audit_id)
> 
> sd_tems_partition_test(# );
> 
> CREATE TABLE
> 
> sd_tems_partition_test=# \d audit_logging.audit_param;
> 
>                       Table "audit_logging.audit_param"
> 
>       Column     |          Type           | Collation | Nullable | Default
> 
> ----------------+-------------------------+-----------+----------+---------
> 
> audit_id       | bigint                  |           | not null |
> 
> param_position | smallint                |           | not null |
> 
> value          | character varying(4096) |           | not null |
> 
> Indexes:
> 
>      "audit_param_pk" PRIMARY KEY, btree (audit_id)
> 
> sd_tems_partition_test=#
> 
> sd_tems_partition_test=# Alter table audit_logging.audit_param add 
> constraint audit_param_audit_fk2 FOREIGN KEY (audit_id) REFERENCES 
> audit_logging.audit_p(id);
> 
> *ERROR:  there is no unique constraint matching given keys for 
> referenced table "audit_p"*

The PK(UNIQUE constraint) on audit_logging.audit_p is:

"audit_pk1" PRIMARY KEY, btree (id, create_dtt)

You are only specifying id:

REFERENCES audit_logging.audit_p(id)
> 
> sd_tems_partition_test=#
> 
> Thanks,
> 
> Shatamjeev
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (6+ messages)  latest in thread

Message-ID: <f9fc06d7-c8c1-31ef-4b8f-107f5385a611@aklaver.com>
Permalink:  ../f9fc06d7-c8c1-31ef-4b8f-107f5385a611@aklaver.com/
Also on:    postgresql.org/message-id/f9fc06d7-c8c1-31ef-4b8f-107f5385a611@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, sdewan@nbsps.com, mlewis@entrata.com
  Subject: Re: ERROR: there is no unique constraint matching given keys for referenced table "audit_p"
  In-Reply-To: <f9fc06d7-c8c1-31ef-4b8f-107f5385a611@aklaver.com>
  References: <F2F23E564EB92A4C80228EB86713BA130BF984@TO1RODC.nbspaymentsolutions.com> <f9fc06d7-c8c1-31ef-4b8f-107f5385a611@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