aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-12-22 13:36:20 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-22 13:47:11 -0700
commita03ea684efc3505647cf0327a501aa2dbb591ad2 (patch)
tree2e3836f0fe873994a5398cce7e782635f9e5077d
parent82afeaf23b0cc190d1ed2c8858a11980c71ac336 (diff)
downloadrails-a03ea684efc3505647cf0327a501aa2dbb591ad2.tar.gz
rails-a03ea684efc3505647cf0327a501aa2dbb591ad2.tar.bz2
rails-a03ea684efc3505647cf0327a501aa2dbb591ad2.zip
Use the new `foreign_key` option on `references` in generators
Changes `rails g model Post user:references` from def change create_table :posts do |t| t.references :user, index: true end add_foreign_key :posts, :users end to def change create_table :posts do |t| t.references :user, index: true, foreign_key: true end end Changes `rails g migration add_user_to_posts user:references` from def change add_reference :posts, :users, index: true add_foreign_key :posts, :users end to def change add_reference :posts, :users, index: true, foreign_key: true end
-rw-r--r--activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb3
-rw-r--r--activerecord/lib/rails/generators/active_record/migration/templates/migration.rb6
-rw-r--r--railties/lib/rails/generators/generated_attribute.rb4
-rw-r--r--railties/test/generators/migration_generator_test.rb5
-rw-r--r--railties/test/generators/model_generator_test.rb16
5 files changed, 20 insertions, 14 deletions
diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
index fb0fbb4759..f7bf6987c4 100644
--- a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
@@ -15,8 +15,5 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<% attributes_with_index.each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<% end -%>
-<% attributes.select(&:reference?).reject(&:polymorphic?).each do |attribute| -%>
- add_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
-<% end -%>
end
end
diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
index 7df9bcad25..ae9c74fd05 100644
--- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -4,9 +4,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<% attributes.each do |attribute| -%>
<%- if attribute.reference? -%>
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
- <%- unless attribute.polymorphic? -%>
- add_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
- <%- end -%>
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%>
@@ -29,9 +26,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<%- if migration_action -%>
<%- if attribute.reference? -%>
remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
- <%- unless attribute.polymorphic? -%>
- remove_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
- <%- end -%>
<%- else -%>
<%- if attribute.has_index? -%>
remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb
index f16bd8e082..77a3dbf9a2 100644
--- a/railties/lib/rails/generators/generated_attribute.rb
+++ b/railties/lib/rails/generators/generated_attribute.rb
@@ -159,6 +159,10 @@ module Rails
options.delete(:required)
options[:null] = false
end
+
+ if reference? && !polymorphic?
+ options[:foreign_key] = true
+ end
end
end
end
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index c2c8e2abad..0803699968 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -189,8 +189,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
- assert_match(/add_foreign_key :books, :authors/, change)
- assert_no_match(/add_foreign_key :books, :distributors/, change)
+ assert_match(/add_reference :books, :author,.*\sforeign_key: true/, change)
+ assert_match(/add_reference :books, :distributor/, change) # sanity check
+ assert_no_match(/add_reference :books, :distributor,.*\sforeign_key: true/, change)
end
end
end
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb
index ac7a0acf6b..9dc438fe3c 100644
--- a/railties/test/generators/model_generator_test.rb
+++ b/railties/test/generators/model_generator_test.rb
@@ -407,13 +407,23 @@ class ModelGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_foreign_key_is_not_added_for_non_references
+ run_generator ["account", "supplier:string"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_no_match(/foreign_key/, up)
+ end
+ end
+ end
+
def test_foreign_key_is_added_for_references
run_generator ["account", "supplier:belongs_to", "user:references"]
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
- assert_match(/add_foreign_key :accounts, :suppliers/, up)
- assert_match(/add_foreign_key :accounts, :users/, up)
+ assert_match(/t\.belongs_to :supplier,.*\sforeign_key: true/, up)
+ assert_match(/t\.references :user,.*\sforeign_key: true/, up)
end
end
end
@@ -423,7 +433,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
- assert_no_match(/add_foreign_key :accounts, :suppliers/, up)
+ assert_no_match(/foreign_key/, up)
end
end
end