Inserting default values into execute_values

From: Stephen Lagree <stephen(dot)lagree(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: Inserting default values into execute_values
Date: 2020-03-31 22:27:05
Message-ID: CADkZaxXqU+c6_GnW_L0SOgc63iYHomm0RVSpb_+Kww5scVyA4w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

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)"

Thanks,
Steve

Responses

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2020-03-31 22:57:31 Re: Inserting default values into execute_values
Previous Message Daniele Varrazzo 2020-03-30 10:53:17 Re: psycopg3: a first report