-
Notifications
You must be signed in to change notification settings - Fork 274
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
Issue with dynamically building condition clause #813
Comments
try debugging the query that the client creates, maybe might be helpful to understand if your conditions are correct.
put this in the postgres initialization options |
Here is my test results
The generated sql is
The generated sql is
The generated sql is
|
the filter fragments are being generated as promises instead of SQL query fragments. maybe await before the fragments can help? |
I tried that too, but got error |
bumping this, getting exactly the [object Promise] issue. |
Bumping as well - pretty big deal breaker sadly :( |
Currently this requires you to use const filters = [
'foo IN ('nice')',
'foo IN ('cool')
]
await sql`
SELECT foo FROM bar
${sql.unsafe(WHERE ${filters.join (' AND ')})}
` @porsager I'm not an expert in either lib, but it seems the library-level fix for this would be a sql-safe What do you think? |
So the problem you've got is that Array.prototype.join returns a string, and sql is not just strings. You need to use a join that correctly handles sql and parameters. You need each part to be an sql fragment, rather than a string. Look at the code in issue #807 if you're unsure. (Note: Do not use unsafe or await for this; they both will have unexpected outcomes.) |
I try to build a dynamic query with dynamic filters within a transaction but does not work. It returns 0 results.
Here is my code
I also tried below but does not work either
If I replace the expression with the actual sql string it will work
Any idea why?
The text was updated successfully, but these errors were encountered: