You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using where('foo', 'in', []) I would expect the query builder to not produce invalid SQL. This is the behavior with knex:
constquery=knex.select("id","last_name").from("user").whereIn("last_name",[])query.toSQL().sql// 'select "id", "last_name" from "user" where 1 = ?'// bindings: [ 0 ]
Having to check the length of the array to conditionally add the where clause considerably increases the complexity of the query specially in situations where you have more than a simple where:
// The desired APIconstusers=awaitky.selectFrom("users").selectAll().where((eb)=>eb(sql`lower(email)`,"in",emails).or("phone","in",phones)).execute()// What needs to be done todayconstusers=awaitky.selectFrom("users").selectAll().where((eb)=>{constors: Expression<SqlBool>[] = []
if (emails.length) {ors.push(eb(sql`lower(email)`,"in",emails))}
if (phones.length) {ors.push(eb("phone","in",phones))}
return eb.or(ors)
}).execute()
FWIW kysely already does something similar when both conditions in the above query are false:
constemails=[]constphones=[]constquery=ky.selectFrom("users").selectAll().where((eb)=>{constors: Expression<SqlBool>[] = []
if (emails.length) {ors.push(eb(sql`lower(email)`,"in",emails))}
if (phones.length) {ors.push(eb("phone","in",phones))}
return eb.or(ors)
}).compile()query.sql// 'select * from "users" where 1 = 0'
The text was updated successfully, but these errors were encountered:
Hi @koskimas first of all I appreciate the work on the library, I don't think this issue is a duplicate. The other issue I linked was related to accepting anything other than an array as the 3rd argument, and the comment I linked to was related to providing type errors for empty arrays which you said is not worth it and I agree.
What I am proposing here is that kysely correctly fixes the query output when an empty array is provided to where in because it is a cognitive overload to always be aware of empty arrays when using where (which is almost aways possible when doing some filtering). Let me know if you still think this is not common enough and should be handled on the user side, but if you think the proposed behavior makes sense then I would happly try to open a PR
Hey thanks for commenting about the plugin, I found the PR 🙏 Sorry for not finding the other issue (#709) before, for some reason it didn't show up the first time I searched it. Having this as a plugin would be great, for the time being I am using this:
Related issue comment: #447 (comment)
Reproduction of the issue: https://old.kyse.link/?p=s&i=rdvxZDgcBQ7FzTmMG4Cx
When using
where('foo', 'in', [])
I would expect the query builder to not produce invalid SQL. This is the behavior with knex:Having to check the length of the array to conditionally add the
where
clause considerably increases the complexity of the query specially in situations where you have more than a simplewhere
:FWIW kysely already does something similar when both conditions in the above query are
false
:The text was updated successfully, but these errors were encountered: