5

Diesel's SqliteBackend does not implement the SupportsReturningClause trait, so the get_result method cannot be used to retrieve a newly created value.

Is there another way to find out the id of the inserted row? Python has a solution for this. The only solution I've found so far is to use a UUID for ids instead of an autoincrement field.

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Maxim Gritsenko
  • 1,804
  • 8
  • 19
  • 1
    This question does not make much sense as it is currently worded, as it is missing nearly all relevant information like what code you've tried and what dependency versions you use. Additionally there is no `SqlBackend` in diesel, therefore it is not really clear what you are trying to do. I vote for closing this question if it's not improved by the OP. – weiznich Jan 04 '21 at 09:21
  • @weiznich yep, missed "lite" in the name of the backend. Fixed in the question. As for the other questions: all the dependencies are the most recent version. I've tried the `get_result` method (as stated in the question) which does not work, and I don't know if there is anything else I can try. That's the reason I posted the question :-) – Maxim Gritsenko Jan 05 '21 at 16:19

1 Answers1

5

The underlying issue here is that SQLite does not support SQL RETURNING clauses which would allow you to return the auto generated id as part of your insert statement.

As the OP only provided a general question I cannot show examples how to implement that using diesel.

There are several ways to workaround this issue. All of them require that you execute a second query.

  1. Order by id and select just the largest id. That's the most direct solution. It shows directly the issues with doing a second query, as there can be a racing insert at any point in time, so that you can get back the wrong id (at least if you don't use transactions).

  2. Use the last_insert_rowid() SQL function to receive the row id of the last inserted column. If not configured otherwise those row id matches your autoincrement primary integer key. On diesel side you can use no_arg_sql_function!() to define the underlying sql function in your crate.

weiznich
  • 1,808
  • 8
  • 15