diff options
author | Andrey Voronkov <andrey.voronkov@medm.com> | 2015-04-08 19:28:45 +0300 |
---|---|---|
committer | Andrey Voronkov <andrey.voronkov@medm.com> | 2015-04-08 19:28:45 +0300 |
commit | 1a36be390e83156940e6bd3e394152496a6c8e72 (patch) | |
tree | f72b26bae158456f12fea0a46ef011cd888ccc82 | |
parent | 0a120a818d413c64ff9867125f0b03788fc306f8 (diff) | |
download | rails-1a36be390e83156940e6bd3e394152496a6c8e72.tar.gz rails-1a36be390e83156940e6bd3e394152496a6c8e72.tar.bz2 rails-1a36be390e83156940e6bd3e394152496a6c8e72.zip |
Fix uniqueness validation with out of range value
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 25 |
3 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6312960819..305dc01b22 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Uniqueness validation with out of range value fixed + + *Andrey Voronkov* + * MySQL: `:charset` and `:collation` support for string and text columns. Example: diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 9be4b10a55..5106f4e127 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -76,6 +76,8 @@ module ActiveRecord klass.connection.case_sensitive_comparison(table, attribute, column, value) end klass.unscoped.where(comparison) + rescue RangeError + klass.none end def scope_relation(record, table, relation) diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 062bc733f9..778697e225 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -34,6 +34,19 @@ class TopicWithUniqEvent < Topic validates :event, uniqueness: true end +class BigIntTest < ActiveRecord::Base + PG_MAX_INTEGER = 2147483647 + self.table_name = 'cars' + validates :engines_count, uniqueness: true, inclusion: { in: 0..PG_MAX_INTEGER } +end + +class BigIntReverseTest < ActiveRecord::Base + PG_MAX_INTEGER = 2147483647 + self.table_name = 'cars' + validates :engines_count, inclusion: { in: 0..PG_MAX_INTEGER } + validates :engines_count, uniqueness: true +end + class UniquenessValidationTest < ActiveRecord::TestCase fixtures :topics, 'warehouse-things' @@ -86,6 +99,18 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert t2.errors[:title] end + if current_adapter? :PostgreSQLAdapter + def test_validate_uniqueness_when_integer_out_of_range + entry = BigIntTest.create(engines_count: (BigIntTest::PG_MAX_INTEGER + 1)) + assert_equal entry.errors[:engines_count], ['is not included in the list'] + end + + def test_validate_uniqueness_when_integer_out_of_range_show_order_does_not_matter + entry = BigIntReverseTest.create(engines_count: (BigIntTest::PG_MAX_INTEGER + 1)) + assert_equal entry.errors[:engines_count], ['is not included in the list'] + end + end + def test_validates_uniqueness_with_newline_chars Topic.validates_uniqueness_of(:title, :case_sensitive => false) |