Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y0E5E-0001C0-Vg for pgsql-sql@arkaria.postgresql.org; Sun, 14 Dec 2014 18:39:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y0E5C-0007Mj-TR for pgsql-sql@arkaria.postgresql.org; Sun, 14 Dec 2014 18:39:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0E5B-0007Mc-La for pgsql-sql@postgresql.org; Sun, 14 Dec 2014 18:39:53 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y0E53-0005Wg-Gc for pgsql-sql@postgresql.org; Sun, 14 Dec 2014 18:39:51 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 379482063E for ; Sun, 14 Dec 2014 13:39:44 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute2.internal (MEProxy); Sun, 14 Dec 2014 13:39:44 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= x-sasl-enc:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=gYPzjxqWbR+BI60K/w7lgpcOFbo=; b=MaoriuvwhoIv/QilSs jrK1Yqd2F4GN+OJ0ds+FG8INybIkpNOLhnUPuTpA13Nr06MxxXBHR/wjBaqNKIMX LyV0dCsMkw0CQSLXkyECf5X42I5p6NVsbRaq09eihSr37npNRugEZkWhldrorivE JC/9CTNv7fnMZTrRhk6pl9DjI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=gYPzjxqWbR+BI60K/w7lgp cOFbo=; b=ZuMxW/sl9NtEpMm+8lu6Pi8dIkppACE+TXfbbfJCDqm/o/1XhjeUPr 76L9+LFbOOr5aKmVw4psMYWxYfSODRgxPBLRX393AdfzG8OJr+363GAo0//A4C8Y UC6eUc/r2MBJAQsHyw200ko++iVuIVeZA5A8LNWPDaFzjhptY5UtQ= X-Sasl-enc: lBX+Bp1CA8G4fAyuu+nPYsBBI8k+pzt7Z8k3eYScQEFh 1418582383 Received: from [192.168.1.2] (unknown [174.21.228.52]) by mail.messagingengine.com (Postfix) with ESMTPA id B0506C00282; Sun, 14 Dec 2014 13:39:43 -0500 (EST) Message-ID: <548DD96E.7060906@aklaver.com> Date: Sun, 14 Dec 2014 10:39:42 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Ed Rahn , pgsql-sql@postgresql.org Subject: Re: foregin table insert error References: <548D54C9.3040005@gmail.com> <548D9F19.9080405@aklaver.com> <548DBFB3.1090304@gmail.com> In-Reply-To: <548DBFB3.1090304@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 12/14/2014 08:49 AM, Ed Rahn wrote: > On 12/14/2014 09:30 AM, Adrian Klaver wrote: >> On 12/14/2014 01:13 AM, Ed Rahn wrote: >>> Hi, >>> I have a foreign table that I'm getting an insert error on: >>> >>> horsedata=# insert into remote_cache (entry_id, name_id) values(2,1); >>> ERROR: null value in column "id" violates not-null constraint >>> DETAIL: Failing row contains (null, 1, 2, null). >>> CONTEXT: Remote SQL command: INSERT INTO public.cache(id, name_id, >>> entry_id, value) VALUES ($1, $2, $3, $4) >>> >>> >>> Here is the remote table client side: >>> horsedata=# \d remote_cache >>> Foreign table "public.remote_cache" >>> Column | Type | Modifiers | FDW Options >>> ----------+---------+-----------+------------- >>> id | integer | | >>> name_id | integer | | >>> entry_id | integer | | >>> value | integer | | >>> Server: home >>> FDW Options: (table_name 'cache') >>> >>> >>> And here's cache server side: >>> horsedata=# \d cache; >>> Table "public.cache" >>> Column | Type | Modifiers >>> ----------+------------------+---------------------------------------------------- >>> >>> >>> id | integer | not null default >>> nextval('cache_id_seq'::regclass) >>> name_id | integer | >>> entry_id | integer | >>> value | double precision | >>> Indexes: >>> "cache_pkey" PRIMARY KEY, btree (id) >>> "cache_name_id_entry_id_key" UNIQUE CONSTRAINT, btree (name_id, >>> entry_id) >>> "ix_cache_entry_id" btree (entry_id) >>> "ix_cache_name_id" btree (name_id) >>> >>> >>> Any suggestions? >> >> Yes, see here: >> >> http://www.postgresql.org/message-id/CA+mi_8bfkaFPNPPx6_W_T_0J9OEMSfXQKCDZo=OMJpWWcCKtoA@mail.gmail.com >> >> > I tried something similar using -1 as well as the above: > On server I set: > create function inc_id_cache() returns trigger as $inc$ > begin > if NEW.id = NULL then > NEW.id := nextval('cache_id_seq'); > end if; > return NEW; > end; > $inc$ language plpgsql; > > create trigger inc before insert on cache for each row execute procedure > inc_id_cache(); > > > Now in both cases I get: > horsedata=# insert into remote_cache(name_id, entry_id) values (1, 8); > ERROR: null value in column "id" violates not-null constraint > DETAIL: Failing row contains (null, 1, 8, null). > CONTEXT: Remote SQL command: INSERT INTO public.cache(id, name_id, > entry_id, value) VALUES ($1, $2, $3, $4) Hmm, the NOT NULL constraint is being checked before the trigger is run. Maybe change the function to look for a dummy value, say -1 and then use that in your INSERT: insert into remote_cache (entry_id, name_id) values(-1, 2,1); > > thanks > Ed > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql