diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-11 11:16:31 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-26 22:03:49 +0200 |
commit | 9ae1a2c69f51a9065090a9c505f4d22ffbb84094 (patch) | |
tree | 31ed0a7a6935e26c8a98809678eb12bede9dc68c /activerecord | |
parent | acd0287dc18a3fbba6fa4301cb31a7aecd22922b (diff) | |
download | rails-9ae1a2c69f51a9065090a9c505f4d22ffbb84094.tar.gz rails-9ae1a2c69f51a9065090a9c505f4d22ffbb84094.tar.bz2 rails-9ae1a2c69f51a9065090a9c505f4d22ffbb84094.zip |
fk: raise when identifiers are longer than `allowed_index_name_length`.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/migration/foreign_key_test.rb | 9 |
2 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 18e73b0200..5a863717e5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -682,7 +682,11 @@ module ActiveRecord def foreign_key_name(table_name, options) # :nodoc: options.fetch(:name) do - "#{table_name}_#{options.fetch(:column)}_fk" + identifier = "#{table_name}_#{options.fetch(:column)}_fk" + if identifier.length > allowed_index_name_length + raise ArgumentError, "Foreign key name '#{identifier}' is too long; the limit is #{allowed_index_name_length} characters" + end + identifier end end diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb index c69fc18d82..815c6b2955 100644 --- a/activerecord/test/cases/migration/foreign_key_test.rb +++ b/activerecord/test/cases/migration/foreign_key_test.rb @@ -136,6 +136,15 @@ module ActiveRecord assert_equal :nullify, fk.on_update end + def test_add_foreign_key_with_too_long_identifier + with_example_table @connection, "long_table_name_will_result_in_a_long_foreign_key_name", "rocket_id integer" do + e = assert_raises(ArgumentError) do + @connection.add_foreign_key "long_table_name_will_result_in_a_long_foreign_key_name", "rockets" + end + assert_match(/^Foreign key name 'long_table_name_will_result_in_a_long_foreign_key_name_rocket_id_fk' is too long;/, e.message) + end + end + def test_remove_foreign_key_inferes_column @connection.add_foreign_key :astronauts, :rockets |