提交 51eacc71 编写于 作者: Y Yves Senn

Merge pull request #19652 from vngrs/pluralize_foreign_key_table_name_bug

use singular table name if pluralize_table_names is setted as false whil...
* Foreign key related methods in the migration DSL respect
`ActiveRecord::Base.pluralize_table_names = false`.
Fixes #19643.
*Mehmet Emin İNAÇ*
* Reduce memory usage from loading types on pg.
Fixes #19578.
......
......@@ -138,7 +138,7 @@ def column_names
end
def foreign_table_name
name.to_s.pluralize
Base.pluralize_table_names ? name.to_s.pluralize : name
end
end
......
......@@ -667,7 +667,10 @@ def add_reference(table_name, *args)
# remove_reference(:products, :user, index: true, foreign_key: true)
#
def remove_reference(table_name, ref_name, options = {})
remove_foreign_key table_name, ref_name.to_s.pluralize if options[:foreign_key]
if options[:foreign_key]
reference_name = Base.pluralize_table_names ? ref_name.to_s.pluralize : ref_name
remove_foreign_key(table_name, reference_name)
end
remove_column(table_name, "#{ref_name}_id")
remove_column(table_name, "#{ref_name}_type") if options[:polymorphic]
......
......@@ -105,6 +105,28 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase
@connection.remove_reference :testings, :testing_parent, foreign_key: true
end
end
test "foreign key methods respect pluralize_table_names" do
begin
original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names
ActiveRecord::Base.pluralize_table_names = false
@connection.create_table :testing
@connection.change_table :testing_parents do |t|
t.references :testing, foreign_key: true
end
fk = @connection.foreign_keys("testing_parents").first
assert_equal "testing_parents", fk.from_table
assert_equal "testing", fk.to_table
assert_difference "@connection.foreign_keys('testing_parents').size", -1 do
@connection.remove_reference :testing_parents, :testing, foreign_key: true
end
ensure
ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names
@connection.drop_table "testing", if_exists: true
end
end
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册