Weird Query builder behavior (extras)
Hello! I have experiencing weird situation while using Drizzle query builder.
My code is the following:
The question is: why Drizzle generates different query, replacing table name in some cases?
Maybe i am doing something wrong?
Thanks in advance!
(drizzle-orm: 0.29.3, node 20)
17 Replies
Can you try the extras like this?
Or better yet, define the subquery outside
Thanks for your answer, but unfortunately it didn't help much:
1. TypeScipt linter arguing:
2. Also, there is runtime error:
Of all the error I thought it would show, that was not one of them
import * as db from "drizzle-orm/pg-core
??i tried to remove
.as("avgPrice").avgPrice
and added .getSQL().as('avgPrice')
, then query builds, but it looks like this:
what do you mean by this message?I just thought it was weird to name it that, but I see you probably did that only in the schema file
yeah, sorry for awkward naming
I guess try it like this. Typescript playground
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
now generated query looks like this:
lol
I guess you'll have to use crud API
oh, finally! i changed your code a little bit and now it produces valid SQL!
produced SQL:
I will try it out on bigger query and write down if everything is fine or not.
Thanks again for your help!
Damn, I know that 🤦♂️
😀
yeap, everything is fine now.
This example helped me alot thank you, but If you don't mind I have a question. In my case it doesn't return number it returns an an array of objects.
Did you by any chance find a way around this?
I am not sure, but it can happens because of
Try to remove
await
, and check againYou were right, it worked thank you! But why does this happen though, is this a js Promises thing or a drizzle thing? I am not sure I understand why.
I am not sure how exactly it works under the hood, but all "queries" that you
await
- will be executed and you will receive an execution result, but if you will not await
- it can be used as a subquery later in your code.
I am not sure though is it possible to store them separately or not (like, create special file with all subqueries and import one of them then it needed)I am still very curious and will probably try to learn more about this later. Thank you for your help though. Will let you know if I learn anything.