1
votes

I wanted to do something like this:

    async with app.pg_pool.acquire() as pg:
        uid = await pg.execute('INSERT INTO users (created, keyed, key, email) '
                               'VALUES ($1, $2, $3, $4) RETURNING id',
                               time, time, key, rj['email'])['id']

However Connection.execute doesn't seem to return anything other than the status:

https://magicstack.github.io/asyncpg/current/api/index.html?highlight=returning#asyncpg.connection.Connection.execute

The question could be otherwise phrased as: How do I get back the response of the RETURNING statement when using asyncpg?

2

2 Answers

0
votes

The Connection.execute will return the status of the INSERT. If you need the Record, Records or Value use one of the following instead of Connection.execute :

  1. Connection.fetchval - returns only the value of one field specified. Returns null if no fields are specified.
  2. Connection.fetch - will return an array of dict. Each dict will have the list of fields specified after the RETURNING statement.
  3. Connection.fetchrow - will return a dict with the list of fields specified after the RETURNING statement