Get `id` of inserted row
Is this the right way to get the id of an insert? It feels incredibly cumbersome for such a common pattern, so feel like I'm doing something wrong:
17 Replies
Bump
well you can skip creating a return type
Then you're selecting the entire object though
i fail to see the issue here
Selecting only the columns you need vs all columns improves performance, CPU usage, and billing
The way you handle the response from your driver doesn't have anything to do with DB reads. What is your driver BTW?
Doesn't the insert method return an object with
insertId
already tho?I'm assuming your using pg.
Doesn't returning(...) affect the DB read?
Yeah, pg
Oh, I get it now. the returning method lets you select what you want, but i'm not sure if that is implemented in the DB level
Exactly. Sounds like this is the shortest syntax, which works, but is too verbose IMO
It looks like this is implemented at the DB level. https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/dialect.ts#L375
GitHub
drizzle-orm/drizzle-orm/src/pg-core/dialect.ts at main · drizzle-te...
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
So yeah! that is the syntax you should use
Now, I'm not super familiar with pg. But, don't you get the
insertedId
from the insert method without the returning?
That is the case for mysql but I don't know in pg.Oh ok, good to know haha
According to the docs, you do not get this for MySQL https://orm.drizzle.team/docs/crud#insert-returning
Querying with SQL-like syntax [CRUD] – DrizzleORM
Drizzle ORM | %s
Yeah, returning is not for MySQL, however
insertId
is for MySQL exclusively
weird ¯\_(ツ)_/¯There you go then!