executemany not discarding result aka fetchmany #137
Copy link
Copy link
Closed
Labels
Description
With the following query
INSERT INTO people (name) VALUES ($1)
RETURNING idand values to create [('anne',), ('ben',), ('charlie',)].
I believe there's currently no way with asyncpg to execute this single query and get all the ids returned.
I've tried:
executemanybut the result is discardedfetchbut that doesn't extend to executing manyfetchwhile casting the argument to an array::TEXT[][]but I either get a syntax error or a single value of the stringified list insert.
In short: would it be possible for executemany to return the result?
Reacted by Felix Meyer-Wolters, Artem, komendantyan and Nikita Melentev
Activity
elprans commentedon May 11, 2017
Try this:
samuelcolvin commentedon May 11, 2017
Thanks, that got me to the solution, I actually needed
(Note the
::TEXT[])Can I suggest a new section for docs "example of sql with asyncpg"? I know this is technically an issue with my knowledge of postgres not asyncpg, but such a section would ease the learning curve of using asyncpg a lot and avoid you having to answer these questions.
elprans commentedon May 11, 2017
Yes, I'm actually working on it right now.
jaredvacanti commentedon Mar 28, 2018
Is it possible to accomplish this same
executemanywithRETURNINGwhile passing in a list of dicts/mappings?For the pyscopg2 alternative, it would be using
psycopg2.extras.execute_valueswhich requires a template of the columns. This then supports default values.feluxe commentedon Nov 12, 2018
I'm having the same problem with this query:
INSERT INTO persons (lastname, firstname) VALUES ($1, $2) RETURNING id;I was looking for
fetchmanyintuitively, without luck, so I landed here :) I think afetchmanyfunction would be really handy for situations like this.I ended up looping over the list of persons executing a single
fetchcall for each.jiamo commentedon Nov 8, 2019
Any new progress?
shurshilov commentedon Dec 2, 2024
Any news???
jabajke commentedon Jan 10, 2025
if its possible, you may use asyncpg with jinjasql
we have workaround for such cases
prbly it would be useful for someone
elprans commentedon Jan 10, 2025
This was implemented in #1175 (as
fetchmany).