Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foreign_key respects table_name_prefix and table_name_suffix #24614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def index(column_name, options = {})
end

def foreign_key(table_name, options = {}) # :nodoc:
table_name_prefix = ActiveRecord::Base.table_name_prefix
table_name_suffix = ActiveRecord::Base.table_name_suffix
table_name = "#{table_name_prefix}#{table_name}#{table_name_suffix}"
foreign_keys.push([table_name, options])
end

Expand Down
30 changes: 30 additions & 0 deletions activerecord/test/cases/migration/references_foreign_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase
end
end

class CreateDogsMigration < ActiveRecord::Migration::Current
def change
create_table :dog_owners

create_table :dogs do |t|
t.references :dog_owner, foreign_key: true
end
end
end

def test_references_foreign_key_with_prefix
ActiveRecord::Base.table_name_prefix = 'p_'
migration = CreateDogsMigration.new
silence_stream($stdout) { migration.migrate(:up) }
assert_equal 1, @connection.foreign_keys("p_dogs").size
ensure
silence_stream($stdout) { migration.migrate(:down) }
ActiveRecord::Base.table_name_prefix = nil
end

def test_references_foreign_key_with_suffix
ActiveRecord::Base.table_name_suffix = '_s'
migration = CreateDogsMigration.new
silence_stream($stdout) { migration.migrate(:up) }
assert_equal 1, @connection.foreign_keys("dogs_s").size
ensure
silence_stream($stdout) { migration.migrate(:down) }
ActiveRecord::Base.table_name_suffix = nil
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have one with prefix+suffix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test "multiple foreign keys can be added to the same table" do
@connection.create_table :testings do |t|
t.integer :col_1
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ def up
create_table("dogs") do |t|
t.column :name, :string
t.column :owner_id, :integer
t.index [:name]
t.foreign_key :dog_owners, column: "owner_id" if supports_foreign_keys?
end
add_index "dogs", [:name]
add_foreign_key :dogs, :dog_owners, column: "owner_id" if supports_foreign_keys?
end
def down
drop_table("dogs")
Expand Down