aio-libs / aio-libs/aiopg

Getting inserted primary key / using RETURNING

未關閉
#582 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
1.4k
分支
170
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。