drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[FEATURE]: Return insert id as the result of insert statement

Open ArtemGolovko opened this issue 1 year ago • 1 comments

Describe what you want

For almost all of my project I used MySQL database and autoincrement ids. After executing an insert statement, I expect to get an id of the thing I just have inserted. But it is not the case with drizzle orm, since MySQL does not support returning statement. That means that here is no convenient way to freshly generated id of a row I just inserted.

But it doesn't have to be that way. Both mysql2 and @planetscale/database support returning insert id from insert statement, and I am pretty sure it is also possible in http proxy. As for now, drizzle lacks functionality to return insert id. I suggest adding a new method to drizzle's query builder called .returnInsertId(). The method should return a single value or a list of values depending on how many rows have been inserted.

ArtemGolovko avatar May 20 '24 09:05 ArtemGolovko

      const [res] = await this.drizzle.insert(schema).values({
          data: value
      })

      res.insertId // is this what you are looking for?

arily avatar May 23 '24 11:05 arily

Thank you! That is exactly what I was looking for.

Before posting this issue, I read the hole docs on insert statement and could not find anything useful. I'll make a PR to update the docs soon.

ArtemGolovko avatar May 26 '24 09:05 ArtemGolovko