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 1icZgK-0001Tz-Ke for psycopg@arkaria.postgresql.org; Wed, 04 Dec 2019 18:47:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1icZgJ-0004fq-CF for psycopg@arkaria.postgresql.org; Wed, 04 Dec 2019 18:47:23 +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 1icZgJ-0004Zc-05 for psycopg@lists.postgresql.org; Wed, 04 Dec 2019 18:47:23 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1icZgG-0003GM-BW for psycopg@lists.postgresql.org; Wed, 04 Dec 2019 18:47:21 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id ADF9C226D2; Wed, 4 Dec 2019 13:47:19 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 04 Dec 2019 13:47:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=c btza+ZHfIDBCS8SBqUm9sjMuX2+lRzZl0L02oU8YhA=; b=Edul2XG1Okx4xgLDK +iHVF9duovsjcas+J9GMjcGWHvvXVdcsBaoI2H3RVqY16d0kfUmhvGkg3FSGht32 wxhN52LEzwAvdT0SIGcs5H51LuSSthgGOBBC3/fXHbzAylp54SOHRGGyPYfKUjf7 M2saKpAIxHiXFwy7DNxYqrAevJA4kvGi6e/HbO12fYhXeoI74e64Hg0pYofHFKL6 cc24iOB7xYK5Np9kE2PaI7wAwZNGP8N8NVvXAR7V1rGl+vTUZGEjzuIwlQ/upNBf 0KGBS6ijxb7HEfsUFz5ySKaFwz3t6dcT1EYjAVaZLZa5WwQIwxgQ491OF8TTj64j Xk3fg== 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=cbtza+ZHfIDBCS8SBqUm9sjMuX2+lRzZl0L02oU8Y hA=; b=Ioh0EsNJHgbQSH75lm81XPOmEuBi0YtxgNz8djHCQJZRGTi5vKDFahRwX T53qSq7toNAalcHcNm8Kez4rs9Uj95hkpbjjRjDfkKih19KgDnbagXqiRo/qoZGD Bg6Ek0G5rAhfiSEhfJ0/pGvcqKv5aJ34jWCQKa9zEv/jMk6opmc2RwtvZY7lWehf 62+NmjoSMpAE17Uve2wJn5F5ioWOsmQbuRgfrxlfXQgAy+D5SKP2BXTsA4wbUb62 v1vtedO9XS1IY2la7IT/Jml+73lTVjRhxRASSjv5+edjaRpBOv29hSLtrOszgkzu 8tA4AWAIGCuHGVHmBx4BxZ5mwUKvw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudejledguddugecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffhvfhfkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepihhnihhtugdroh hrghdpphhoshhtghhrvghsqhhlrdhorhhgnecukfhppeejgedrleefrddutdehrddukeei necurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrg hvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (74-93-105-186-washington.hfc.comcastbusiness.net [74.93.105.186]) by mail.messagingengine.com (Postfix) with ESMTPA id F17068005B; Wed, 4 Dec 2019 13:47:18 -0500 (EST) Subject: Re: Array of tuples as a parameter, with type casts From: Adrian Klaver To: Vladimir Ryabtsev Cc: psycopg@lists.postgresql.org References: <25c3598a-698f-8d0e-3659-791253b903ec@aklaver.com> Message-ID: <55fa8796-a91b-6560-604c-83dd77b17980@aklaver.com> Date: Wed, 4 Dec 2019 10:47:18 -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: <25c3598a-698f-8d0e-3659-791253b903ec@aklaver.com> 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 12/4/19 10:44 AM, Adrian Klaver wrote: > On 12/3/19 4:00 PM, Vladimir Ryabtsev wrote: >> It does not change much, the error message just says it now expects >> 'character varying'. >> Anyway bigint problem is the next in the line. > > Yeah it is related to this: > > https://www.postgresql.org/docs/11/functions-array.html > unnest(anyarray)     setof anyelement > > Dealing with anyelement can be tricky. > > I tried out an alternate solution: > > json_query = "select * from json_to_recordset(%s) as t(a timestamp, b > varchar, c bigint)" > > cur.execute(json_query,[Json([{'a': datetime.now().isoformat(), 'b': > 'abc', 'c': 100},{'a': datetime.now().isoformat(), 'b': 'xyz', 'c': > 200}])]) Should have added that Json comes from: from psycopg2.extras import Json http://initd.org/psycopg/docs/extras.html?highlight=json#json-adaptation > > Which results in: > > test_(postgres)# select * from json_to_recordset(E'[{"a": > "2019-12-04T10:40:22.823910", "b": "abc", "c": 100}, {"a": > "2019-12-04T10:40:22.823918", "b": "xyz", "c": 200}]') as t(a timestamp, > b varchar, c bigint) > ; >              a              |  b  |  c > ----------------------------+-----+----- >  2019-12-04 10:40:22.82391  | abc | 100 >  2019-12-04 10:40:22.823918 | xyz | 200 > (2 rows) > >> >> -- >> Regards, >> Vladimir > > -- Adrian Klaver adrian.klaver@aklaver.com