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

Fix select_count edge case of raising an exception #357

Merged
merged 1 commit into from
Apr 26, 2020
Merged

Conversation

jwoertink
Copy link
Member

Fixes #297

In a more complex case, postgres won't return any value when trying to do a count. This causes DB to just raise an exception instead of returning 0.

…eturn any rows which will raise an exception instead of returning a value. Fixes #297
@@ -182,6 +182,8 @@ module Avram::Queryable(T)
def select_count : Int64
query.select_count
exec_scalar.as(Int64)
rescue e : DB::NoResultsError
0_i64
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're currently doing this thing where we log when a query fails. I wonder if it would be strange to still return this value, but also throw a warning log saying something like "Hey, you just ran this query BLAH, but it's not doing what you think it should be....".... In my case, that may flood the logs a bit, but just a thought..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine if I understand this correctly. It seems like the query would not raise if there were items right? In other words the query is valid and it just didn't have results because there weren't any? If that's the case then I think this is great.

Otherwise I almost think this should raise and we have a `select_count!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly. If you have records to return, then you get that number back, but if there's no records, then instead of getting 0, PG gives you nothing. Something to do with how groups work probably. Like, no groups returned, so there was nothing to count on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I think 0 makes sense without a log then. If we do decide to log at some point we can add a new log class so it’s easy to disable/enable. But that can wait until someone wants it. Imo this is the expected behavior so is probably fine

@jwoertink jwoertink merged commit 27edded into master Apr 26, 2020
@jwoertink jwoertink deleted the bugs/297 branch April 26, 2020 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No results on a select_count
2 participants