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 find_in_batches against string IDs when start option is not specified #8073

Conversation

alexisbernard
Copy link

Hi,

Here is a fix for find_in_batches when Model#id is a string. In fact when calling find_in_batches (or find_each) without the start option it generates a wrong SQL statement:

Subscriber.find_each { |subscriber| ...  }
# Generates SQL:
# SELECT  "subscribers".* FROM "subscribers"  WHERE ("subscribers"."nick" >= 0) ...

That statement is invalid with strict databases such as PostgreSQL, because it tries to compare a string against an integer.

With more tolerant databases such as SQLite it's passing, but in fact it skips IDs starting with a lower character than '0'.

Once accepted and merged, I plan to backport that fix in pull request #7987 for rails 3.2.

cc @spastorino

assert_kind_of Subscriber, subscriber
end
end
end

Choose a reason for hiding this comment

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

Wrong indent.

@carlosantoniodasilva
Copy link
Member

It needs a changelog entry as well. thanks.

@alexisbernard
Copy link
Author

Thanks for pointing those details.

@@ -136,4 +136,13 @@ def test_find_in_batches_should_use_any_column_as_primary_key

assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id)
end

def test_find_in_batches_should_use_any_column_as_primary_key_when_start_is_not_specified

Choose a reason for hiding this comment

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

This line is still bad indented.

@carlosantoniodasilva
Copy link
Member

This will need a rebase since github says it cannot be automatically merged anymore, and please squash your commits. Thanks!

@alexisbernard
Copy link
Author

Sorry for the poor indentation. I just rebased master into that branch and squashed my commits :)

carlosantoniodasilva added a commit that referenced this pull request Oct 31, 2012
…_option

Fix find_in_batches against string IDs when start option is not specified
@carlosantoniodasilva carlosantoniodasilva merged commit 92bae49 into rails:master Oct 31, 2012
@carlosantoniodasilva
Copy link
Member

@alexisbernard thanks.

@alexisbernard
Copy link
Author

Awesome, thanks for the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants