Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJPpH-0007o2-2i for psycopg@arkaria.postgresql.org; Tue, 31 Mar 2020 22:57:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJPpC-0003kg-Od for psycopg@arkaria.postgresql.org; Tue, 31 Mar 2020 22:57:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJPpB-0003kN-3G for psycopg@lists.postgresql.org; Tue, 31 Mar 2020 22:57:38 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJPp8-0006LT-6h for psycopg@postgresql.org; Tue, 31 Mar 2020 22:57:36 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 1693C5C0453; Tue, 31 Mar 2020 18:57:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 31 Mar 2020 18:57:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=e Vxz/9KRHLbrNMWn4TLlGNOgFbpuwrJ7jzod3ZYH0kM=; b=sVUtuiEuQyNiJSlZM p9tQHiFjkwAhEEXyUIKexMYPkrD+LmacAO1QaNAy2ralELzZyueKFAlE8nwdyAk0 q0Ns61/eZV4SZ+xMs0rg97IPKA7Z8ArRANLuLNdymYL2USPUX5A8dsYeMy/P8wvT XAsHqjgmED8PelzyKJ8x27IQSvnURR+3006DBBqrlY5gx7yb5yey6I549f7SrXaJ vN3cScFyYoBBtdlVr/bhc903kpiwppHRThT+fPDzFz+7FBvg8t+EKf2U5i6SPezT AfI2tiMNaRJWKYzFGcSA4dsEO8SxZSwKjl3p0FWdcbplfN65HVLzyJhf3w9dO272 vckMg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=fm2; bh=eVxz/9KRHLbrNMWn4TLlGNOgFbpuwrJ7jzod3ZYH0 kM=; b=wde0w7jemwMA7fQKl+lIg6nQqicQUSdLFmCmTDGtDaUFhihzpI3fNXtjl BTs48yNP/LMjUmnCP4DU/wMUu9UQ6U2jPp8QuSItJvmmfKsPzFhpD/QBrl1pKb6M +r4dDT58al1xlSRjQ0RRxNx+/I4tk8EU2+3CDZuppUIlLeMPdU4tDWuqnALvsjsG Rszt7fjeR6+Aw89NnhYFWO0RaCvGSPwlmy9/HNu12ppiU+qfDNEbL1A4rcUAXOQe tnbqVatl7BI914A7w4oXnDWSuCEvJeGghq5asFcQj37QFPWdPhE22pgb8JH3fBkv Z3UWorRXmgZS9YjUvdhYoinxrJcbg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtddtgddugeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjeehrddujedvrdeltddrledvnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvh gvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (75-172-90-92.tukw.qwest.net [75.172.90.92]) by mail.messagingengine.com (Postfix) with ESMTPA id 4A2EB328005E; Tue, 31 Mar 2020 18:57:32 -0400 (EDT) Subject: Re: Inserting default values into execute_values To: Stephen Lagree , psycopg@postgresql.org References: From: Adrian Klaver Message-ID: <6db34cfa-c531-18ff-4368-bea8c171f6db@aklaver.com> Date: Tue, 31 Mar 2020 15:57:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 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 3/31/20 3:27 PM, Stephen Lagree wrote: > Hello, > > I am trying to insert into a table to generate sequential ids.  Is there > a way to do this repeatedly using execute_values if there is only one > column and it is auto incremented? > It seems the execute_values requires at least one non-default value. > > I am trying to do this: >     query = "INSERT INTO MYTABLE (id) VALUES (DEFAULT) RETURNING id;" >     execute_values(cursor, query, args_list, template=None, > page_size=100, fetch=True) > > If I don't use a %s argument and just put dummy values in the arglist, I > get error > E           ValueError: the query doesn't contain any '%s' placeholder > I understand why this doesn't work because it can't extract the > placeholder and replicate values there. > > If I change DEFAULT to %s and try to use blank tuples I get this > E           psycopg2.errors.SyntaxError: syntax error at or near ")" > E           LINE 1: INSERT INTO MYTABLE (id) VALUES (),(),() RETURNING id; > > If I use "DEFAULT" as a string it tries to insert a string into an int > column, not use the DEFAULT value.  Is there a way to insert the default > value here?  I don't see anything like this in the documentation. > > My table looks like this: > "CREATE TABLE MYTABLE (id SERIAL PRIMARY KEY)" A solution from Daniele Varrazzo. I can't find the mailing list post where it appeared, just where I use it in code: Given a file: utilities/psycopg_helpers.py """Psycopg2 helper code. Code for extending psycopg2. """ import psycopg2 class Default(object): """Set up DEFAULT value for a field. When doing INSERT or UPDATE in Postgres one can use DEFAULT/default as the value to have the server use the default set on the field. The below allows for doing that. """ def __conform__(self, proto): if proto is psycopg2.extensions.ISQLQuote: return self def getquoted(self): return 'DEFAULT' DEFAULT = Default() Then import it: from .utilities.psycopg_helpers import DEFAULT and use DEFAULT where you want a SQL DEFAULT. > > Thanks, > Steve -- Adrian Klaver adrian.klaver@aklaver.com