Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ArtemGolovko opened this issue May 20, 2024 · 2 comments
Closed

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

ArtemGolovko opened this issue May 20, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ArtemGolovko
Copy link

ArtemGolovko commented May 20, 2024

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 ArtemGolovko added the enhancement New feature or request label May 20, 2024
@arily
Copy link

arily commented May 23, 2024

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

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

@ArtemGolovko
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants