Update and return a single row only when using .update()?

Hi, I have the following drizzle code which updates levels in a table.
const updatedLevel = await db
.update(levels)
.set(queriedLevel)
.where(
and(
eq(levels.userId, user_id),
eq(levels.guildId, guild_id)
)
)
.returning();
const updatedLevel = await db
.update(levels)
.set(queriedLevel)
.where(
and(
eq(levels.userId, user_id),
eq(levels.guildId, guild_id)
)
)
.returning();
Is it possible to only update a single row in the levels table instead of all that match the where clause? I'm asking this because I want the returning() value to be a single object instead of an array of objects. I'm fairly new to pg and drizzle so I apologise if it's a silly question xD
2 Replies
Luxaritas
Luxaritas13mo ago
All of drizzle’s APIs (except for a specific case for SQLite, iirc) will always return an array even if only a single row is matched This matches the behavior of the underlying database drivers, and generally drizzle doesn’t have a reliable way of knowing that a query can only return one row Know that you’re returning only one row? Get the first element of the returned array, maybe validating that the array length is indeed one to be safe
Avraj
AvrajOP13mo ago
Oh I see. Thanks. I've been getting the first element of the returned array so I think I'll stick with that.
Want results from more Discord servers?
Add your server