11

I've ran a simple insert command:

INSERT INTO names (name) VALUES ('john')

As a response I get a PG::Result object. I've been digging through those docs but I can't squeeze out of that object the info I need: what is the ID of the row I've just inserted?

4

1 回答 1

34
res  = conn.exec("INSERT INTO names (name) VALUES ('john') returning *")
res[0]
res[0]['id']

我曾经returning *只是为了表明您可以返回所有内容,而不仅仅是 id。但显然,如果您只需要 id 或 id 以及其他一些列,请使用显式形式,就像在选择列表中一样

returning id, name
于 2013-07-25T23:09:28.057 回答