-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
when passing empty list in .whereIn()
#2897
Comments
Honestly I think its perfectly fine the way it is. When calling To me this is the same as saying an undefined value in |
@wubzz I agree with your point. thanks for your explanation in detail :) |
I think that throwing error can be a better solution for this situation. In my use case, it makes business logic broken when ignoring empty list and returning nothing. I believe it can be safer when knex throws an error. Users can realize their bugs quickly if we throw errors. How do you think about this? @wubzz |
I still think its wrong to throw an error. You could just simply do this:
Most use-cases consist around argument to This is just my personal opinion, maybe better to get some more input. |
@wubzz I wouldn't call it a breaking change since current behaviour doesn't seem to be documented, and I don't think it's the one that most users would expect. |
@kibertoad If we're evaluating breaking changes as something that must exist in documentation then I guess we are free to change or remove a lot in Knex without a breaking-change warning.. The reason I call it a breaking change is because for some applications that may have used Knex extensively will undoubtedly have used I don't see why @tgriesser What are your thoughts on this now compared to few years ago #477 ? |
@wubzz I'm basing this on precedents within the project; previously @elhigu mentioned that he does not consider changing ambiguous undocumented aspects of functionality a breaking change (e. g. how "schema.tableName" syntax became unsupported without a prior warning in some places). Which actually did break some production code :). I agree that ignoring criteria is also going to be surprising, personally I'm more in favour of throwing an error. |
I feel there's a need to differentiate between accidental and deliberate breaking changes, documented or not. Don't know about the change you're referring to (even if maybe I should) and if it was deliberate or not, but in this case we'd know the outcome beforehand. We seem to agree that silently ignoring is out of the question. The remaining alternative is to throw an error. Neither of these changes solve what is described in the issue example in terms of output. For dynamic lists you would always have to Turns out So I guess my question is: What is to be gained from changing this given the facts above? My two cents is to keep it the way it is, but add it to docs. |
@wubzz I mean #2499 which was caused by 7ff766f - but you are right, there wasn't originally an expectation it will break anything.
Documentation claims otherwise:
But you already need to do this for .where() statements.
Yes, but in all cases you would want to short-circuit call and never hit DB in the first place if you already know at query building time that you are getting 0 results no matter what. Meaning that remaining cases are almost always going to be due to users being negligent. |
Of course, everyone should write code like this However, in my case of using Knex.js for the first time, I didn't know that fact and made a mistake of not checking the Because Knex ignores invalid input and builds query like I believe if Knex throws error when empty list is passed, users can figure out the fact that they need to check the length of list more quickly. Shouldn't we guide users to use Knex.js properly with errors when invalid input is passed? I believe it would be a more generous way than ignore it silently. |
.whereIn()
.whereIn()
Huh? It explains exactly what I meant. Undefined to
Not sure I'm following, I have never done that afaik.
This isn't really an argument, knex could just not issue the query then and return 0 rows immediately since it knows it will always be empty.. @HurSungYun Sure, except I'm gonna drop out of this discussion, think I've said all I can. Change it if you want. Personally think it's a mistake. |
This is actually a feature and it works like this by design. So closing as not a bug. Could be documented though. |
It is not an invalid value like for example
There can be still other where clauses in query which still returns other results and it is pretty common that people are querying multiple |
Right, so we have team majority on the issue and path forward is clear 👍. |
OTOH, |
@ricardograca Yup, that's pretty much how I personally see it, for me empty array is pretty much same thing as undefined in this case. |
Main point is that it was done current way by design and it was not an accident and it is the same way that many other ORMs handle it (like e.g. hibernate). It is just the way Tim wanted it to work originally, because it is more convenient for the end user of knex API.
For average user of knex So it is unexpected in both ways and documentation is the correct place to tell why it behaves the way it does. |
That makes sense as long as it's documented, so I think this should be reopened until the docs are updated. |
@ricardograca I've created separate issue in doc repo for that. |
Is there some shorthand that we could use though? A shorthand that checks the length and then skips the query if the length is falsy? |
Environment
Knex version: 0.15.0
Database + version: mysql 5.7
OS: mac OS X
Bug
I tried this
expected
but actually
I believe it should be either the first one or throwing error.
I'd like to work on this issue.
The text was updated successfully, but these errors were encountered: