The Go database/sql Postgres adapter does not support LastInsertId
. From the docs:
pq不支持Result类型中的LastInsertId()方法 数据库/ SQL。返回INSERT的标识符(或UPDATE或 删除),将Postgres RETURNING子句与标准Query或 QueryRow调用。
But the Go database/sql docs recommend not using Query
to modify the database because it:
保留数据库连接,直到关闭sql.Rows。以来 可能有未读的数据(例如,更多的数据行),连接可以 不被使用。在上面的示例中,连接永远不会 再次发布。
文档还说“您永远不应该这样使用Query()”。
Most people recommend using QueryRow
to do an INSERT
with Postgres if you need the resultant row. I don't fully understand the technical reasoning given by the docs, but its seems to conflict with what I've read. Is it safe and considered good practice to use QueryRow
to do an INSERT
?