1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Don't share options with a reference type column

Sharing `options` causes some unexpected behavior. If `limit: 2` is
specified, this means that 2 bytes integer for a reference id column and
2 chars string for a reference type column. Another example, if
`unsigned: true` is specified, this means that unsigned integer for a
reference id column, but a invalid option for a reference type column.
So `options` should not be shared with a reference type column.

(cherry picked from commit 465357aecf)
This commit is contained in:
Ryuta Kamizono 2017-03-04 16:47:43 +09:00 committed by Andrew White
parent 6c8361aea1
commit 7138b467fe
3 changed files with 20 additions and 7 deletions

View file

@ -1,3 +1,13 @@
* Don't share options between reference id and type columns
When using a polymorphic reference column in a migration, sharing options
between the two columns doesn't make sense since they are different types.
The `reference_id` column is usually an integer and the `reference_type`
column a string so options like `unsigned: true` will result in an invalid
table definition.
*Ryuta Kamizono*
* Fix regression of #1969 with SELECT aliases in HAVING clause. * Fix regression of #1969 with SELECT aliases in HAVING clause.
*Eugene Kenny* *Eugene Kenny*

View file

@ -107,16 +107,12 @@ module ActiveRecord
private private
def as_options(value, default = {}) def as_options(value)
if value.is_a?(Hash) value.is_a?(Hash) ? value : {}
value
else
default
end
end end
def polymorphic_options def polymorphic_options
as_options(polymorphic, options) as_options(polymorphic)
end end
def index_options def index_options

View file

@ -50,6 +50,13 @@ module ActiveRecord
assert column_exists?(table_name, :taggable_type, :string, default: 'Photo') assert column_exists?(table_name, :taggable_type, :string, default: 'Photo')
end end
def test_does_not_share_options_with_reference_type_column
add_reference table_name, :taggable, type: :integer, limit: 2, polymorphic: true
assert column_exists?(table_name, :taggable_id, :integer, limit: 2)
assert column_exists?(table_name, :taggable_type, :string)
assert_not column_exists?(table_name, :taggable_type, :string, limit: 2)
end
def test_creates_named_index def test_creates_named_index
add_reference table_name, :tag, index: { name: 'index_taggings_on_tag_id' } add_reference table_name, :tag, index: { name: 'index_taggings_on_tag_id' }
assert index_exists?(table_name, :tag_id, name: 'index_taggings_on_tag_id') assert index_exists?(table_name, :tag_id, name: 'index_taggings_on_tag_id')