Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upSqlite: There should be a way to obtain an id of last insert #771
Comments
killercup
added
enhancement
sqlite
labels
Feb 28, 2017
This comment has been minimized.
|
You can get the result of |
sgrif
closed this
Mar 1, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
krdln commentedFeb 28, 2017
It is a really crucial feature to be able to obtain an id an inserted object. Postgres backend allows to do that using
RETURINGclause via theget_resultmethod. Sqlite exposeslast_insert_rowidbut it's not reexported by diesel. Without that feature, the sqlite backend is quite unusable for me.I see a few possible solutions here:
last_insert_rowidforSqliteConnection. The connection is notSyncso it shouldn't be a problem.idof the insert/update and implement it for all the backends. Postgres backend could just callget_result. This solution is nice as it allows writing backend-agnostic code.get_resultwork on sqlite only on structs containing just the id. I actually don't know diesel well enough to understand whether this is possible by appropriate implementations ofQueryable.get_resultwork for sqlite by pulling data from the structs that are inserted. It won't work forUPDATEthough. I guess that would require some architectural changes in diesel, but on the other hand, it's the simplest solution wrt porting code between backends.SELECT last_insert_rowid();. Maybe this workarkound could be exposed somewhere in the docs (forLoadDslorSqliteConnection)? Also, I have to admit, I don't know how how to execute raw sql query in diesel, searching forrawyields no results.