How do you create a scalar subquery in drizzle?

I want to create the drizzle analog of select * from table_a where id = (select a_id from table_b where id = <....>) This works in raw SQL, but I'm not sure how to construct the subquery to get only the a_id field to supply in the where condition.
drizzle.select().from(tableA).where(eq(tableA.id, drizzle.select(<WHAT GOES HERE>).from(tableB).where(eq(tableB.id, userInputBId))))
drizzle.select().from(tableA).where(eq(tableA.id, drizzle.select(<WHAT GOES HERE>).from(tableB).where(eq(tableB.id, userInputBId))))
6 Replies
francis
francisOP11mo ago
I can get it to work with { id: tableB.aId } but is that the intended way to do it? It doesn't seem to matter what key I use. Adding any extra columns throws a runtime error of PostgresError: subquery must return only one column
Angelelz
Angelelz11mo ago
Yeap, that's the way The key that you use will just be ignored The keys are used to map the result to the key you provide, but it has nothing to do with the SQL that's produced
francis
francisOP11mo ago
a type thought: would it be worth adding an e.g. scalar subquery type as the output of select(field), and having that be the type input to filter functions? that way it would be a type error to pass in multiple column queries into filters at compile time, rather than exploding at runtime would get rid of the odd "you have to define a key that gets thrown away" DX too
Angelelz
Angelelz11mo ago
Interesting. field would be a drizzle column in this case?
francis
francisOP11mo ago
yes
Angelelz
Angelelz11mo ago
Sure, that could be a cool feature request
Want results from more Discord servers?
Add your server