aio-libs / aio-libs/aiopg

Getting inserted primary key / using RETURNING

Aperta
#582 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
1.4k
Fork
170
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

RETURNING clauses can be added to a statement in sqlalchemy using the returning() function (see https://docs.sqlalchemy.org/en/13/core/dml.html). Expected use in SA would look like this:

```
s = some_statement().returning(table.Foo.name, table.Foo.city)
e = dbc.execute(s)
for name, city in e:
print(name, city)
```
And this works perfectly in my test.

In aiopg.sa the equivalent would involve, e.g.:

```
e = await dbc.execute(s)
```

but, unlike the raw sa case, the print() in this aiopg.sa case prints just the column names (of the columns specified in returning()), not the values of the fields inserted.

I'm most often interested in using this to get the id (pk) of the newly inserted record. In this case, I found that I can do this:

```
e = await dbc.execute(s)
r = await e.fetchone()
print(r.id)
```

In fact, I don't even need to use returning() for this -- my statement can be a simple insert().

I'm wondering: what's the *real* difference, under the hood? Is this fetchone() less efficient than the returning() paradigm (i.e., does it incur another round-trip to the DB)? Is the returning() "failure" a known issue in aiopg.sa? Is that not the preferred way to go? Is it really the preferred way even if all you want is the new pkid?

Scanning other issues, this seems to bear some similarity to #401 (https://github.com/aio-libs/aiopg/issues/401), but I don't see a clear enough answer to my case. Thank you in advance.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.