Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWlIk-0006au-Ju for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2019 17:59:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iWlIj-0004SH-FA for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2019 17:59:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWlIh-0004S8-Ec for pgsql-general@lists.postgresql.org; Mon, 18 Nov 2019 17:59:01 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iWlIe-0004Hr-Bn for pgsql-general@postgresql.org; Mon, 18 Nov 2019 17:58:58 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 0F164703; Mon, 18 Nov 2019 12:58:53 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 18 Nov 2019 12:58:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=m ryTsSOwXkREJbj0Q5eYvZEnZs83ztb2RFZvLRrl9WE=; b=AzORJ5HRgGAo7dOA6 EfYvcA0BVYQW+xXZvyBxnmwoXa9dmJG0Hs5PCpgQA6lBhJo2939zTZJ3TIg+rBTp AE7sfFYuX8uGTKOj3bqsCc4Cd8gKUQOv/UzHuNdk6kP6Et4PhF626q7zmFEgBq/3 X8q1iJDLGk9K/feHXgA4yN1c9EyRspybQKqZPJS7+dj3BVkMQekwBW1mXcRJDo/Y z3qZwoHk/H6Gj/2JPp7sGSYFRdfjoqmEDB+vBeTEWIKvu6n1F+NXk2yF23ajjj4A X7YTEQs6oXhJsCOD+kHV/GGoOGtRkbKJQtCimn1N1zJKq1IhQsij8QCaP5B5/Z9J CcjHQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=mryTsSOwXkREJbj0Q5eYvZEnZs83ztb2RFZvLRrl9 WE=; b=aL734qrmXICTK33DlYoUyDyA82r11qw1CCxSLutK9UTs97AE8e78LzO4v AcjoSnncWbe1lh6xLRXvQRFhAXdVEi4q2oNjFWDx94UF0E0Ut0sVFZEPQqVxHcOs 0nnW2pOZJ/6BumPWaTHAd18mn9OFj04ydPEGzduj+wAHMybgaHUipgWoP2DjjRnW kJJwXXBKZ4kf7y4upyhl79aOwtuA8ToJ37TJuoHbDxTaOJ4rYU62E1e8mSFv7ut4 O94qy5zI3WtnWSqDRVVirGRp238gO6Aw+Pi9iFxoSNdMKmpKXbSghm8ZlE/kNpqF U3bad17aYcTtZ8qZgvV1q21wSUklw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudegiedgkeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphephedtrdduleejrdektddrvdenucfrrghrrghmpehmrghilhhfrhhomhep rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghruf hiiigvpedt X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 6E56F8005A; Mon, 18 Nov 2019 12:58:52 -0500 (EST) Subject: Re: ERROR: there is no unique constraint matching given keys for referenced table "audit_p" To: Shatamjeev Dewan , Michael Lewis Cc: pgsql-general References: From: Adrian Klaver Message-ID: Date: Mon, 18 Nov 2019 09:58:51 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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