mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'fix_uniqueness_validation_when_value_is_out_of_range'
This commit is contained in:
commit
67c2deeb4b
3 changed files with 32 additions and 0 deletions
|
@ -1,3 +1,8 @@
|
|||
* Fixed a bug where uniqueness validations would error on out of range values,
|
||||
even if an validation should have prevented it from hitting the database.
|
||||
|
||||
*Andrey Voronkov*
|
||||
|
||||
* MySQL: `:charset` and `:collation` support for string and text columns.
|
||||
|
||||
Example:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -34,7 +34,22 @@ class TopicWithUniqEvent < Topic
|
|||
validates :event, uniqueness: true
|
||||
end
|
||||
|
||||
class BigIntTest < ActiveRecord::Base
|
||||
INT_MAX_VALUE = 2147483647
|
||||
self.table_name = 'cars'
|
||||
validates :engines_count, uniqueness: true, inclusion: { in: 0..INT_MAX_VALUE }
|
||||
end
|
||||
|
||||
class BigIntReverseTest < ActiveRecord::Base
|
||||
INT_MAX_VALUE = 2147483647
|
||||
self.table_name = 'cars'
|
||||
validates :engines_count, inclusion: { in: 0..INT_MAX_VALUE }
|
||||
validates :engines_count, uniqueness: true
|
||||
end
|
||||
|
||||
class UniquenessValidationTest < ActiveRecord::TestCase
|
||||
INT_MAX_VALUE = 2147483647
|
||||
|
||||
fixtures :topics, 'warehouse-things'
|
||||
|
||||
repair_validations(Topic, Reply)
|
||||
|
@ -86,6 +101,16 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
|||
assert t2.errors[:title]
|
||||
end
|
||||
|
||||
def test_validate_uniqueness_when_integer_out_of_range
|
||||
entry = BigIntTest.create(engines_count: INT_MAX_VALUE + 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: INT_MAX_VALUE + 1)
|
||||
assert_equal entry.errors[:engines_count], ['is not included in the list']
|
||||
end
|
||||
|
||||
def test_validates_uniqueness_with_newline_chars
|
||||
Topic.validates_uniqueness_of(:title, :case_sensitive => false)
|
||||
|
||||
|
|
Loading…
Reference in a new issue