Re: insert DEFAULT value

From: Hans Ginzel <hans(at)matfyz(dot)cz>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: insert DEFAULT value
Date: 2021-03-04 05:43:28
Message-ID: 20210304054327.GL11758@artax.karlin.mff.cuni.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

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.

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2021-03-04 10:33:45 Re: insert DEFAULT value
Previous Message Adrian Klaver 2021-03-03 23:23:28 Re: insert DEFAULT value