public inbox for [email protected]  
help / color / mirror / Atom feed
From: Hans Ginzel <[email protected]>
To: Adrian Klaver <[email protected]>
Cc: [email protected]
Subject: Re: insert DEFAULT value
Date: Thu, 4 Mar 2021 06:43:28 +0100
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>

On Wed, Mar 03, 2021 at 03:23:28PM -0800, Adrian Klaver wrote:
>On 3/3/21 1:51 PM, Hans Ginzel wrote:
>You need to do something like:
>cur.execute(sql.SQL("INSERT INTO test_default VALUES ({})").format(DEFAULT))

Formating values into SQL statement is done by the execute(), isn't it?

I like the solution from
https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html

https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html

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.
   """
   def __conform__(self, proto):
     if proto is psycopg2.extensions.ISQLQuote:
       return self
   def getquoted(self):
     return 'DEFAULT'

DEFAULT = Default()

cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,))

Could this be added to the psycopg2 code, please?

H.





view thread (4+ messages)  latest in thread

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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: insert DEFAULT value
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox