Skip to content
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

whereIn crashes on empty list #266

Closed
IlyaSemenov opened this issue May 20, 2024 · 2 comments
Closed

whereIn crashes on empty list #266

IlyaSemenov opened this issue May 20, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@IlyaSemenov
Copy link
Contributor

Similar to #260, I am putting this boilerplate here and there:

return postIds.length ? await db.post.whereIn("id", postIds) : []

because if I don't, it crashes with:

SELECT ... FROM "post" WHERE "post"."id" IN ()
Error: syntax error at or near ")"

Do you think it's worth a similar shortcut on the ORM level (empty list = no query), or that's too magicky? What about subqueries? Technically whereIn([]) could be translated internally into whereSql`false` but that's probably taking on too much...

@romeerez
Copy link
Owner

romeerez commented May 20, 2024

I agree, implementing the same "none" query as in #260 is a bit magical (non-obvious), but anyway it's better than crashing with an SQL error.

Good point regarding sub-queries, need to support expected behavior in sub-queries as well.

db.person.find(123).select({
  pets: (q) => q.pets.whereIn('name', []),
})

Here I'd expect that person is returned with empty array of pets.

But there is a join that makes an INNER JOIN internally, so the relation becomes required:

db.person.find(123).select({
  pets: (q) => q.pets.join().whereIn('name', []),
})

And this would need to throw a not found, since it's what find does when can't find.
Or to return undefined for findOptional.

And need to consider cases when joining and using sub-queries in different ways.

romeerez added a commit that referenced this issue May 25, 2024
@romeerez romeerez added the enhancement New feature or request label May 25, 2024
@romeerez
Copy link
Owner

Done, published in new orchid-orm version.

Empty whereIn will be resolved into none query.
The none now will handle sub-selects and joins better, see the updated docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants