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

Backport to 4-2-stable "Let t.foreign_key use the same to_table twice" #23740

Merged
merged 1 commit into from
Feb 23, 2016
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
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix a bug where using `t.foreign_key` twice with the same `to_table` within
the same table definition would only create one foreign key.

*George Millo*

* Fix regression in dirty attribute tracking after #dup. Changes to the
clone no longer show as changed attributes in the original object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TableDefinition
def initialize(types, name, temporary, options, as = nil)
@columns_hash = {}
@indexes = {}
@foreign_keys = {}
@foreign_keys = []
@native = types
@temporary = temporary
@options = options
Expand Down Expand Up @@ -289,7 +289,7 @@ def index(column_name, options = {})
end

def foreign_key(table_name, options = {}) # :nodoc:
foreign_keys[table_name] = options
foreign_keys.push([table_name, options])
end

# Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def create_table(table_name, options = {})
end
end

td.foreign_keys.each_pair do |other_table_name, foreign_key_options|
td.foreign_keys.each do |other_table_name, foreign_key_options|
add_foreign_key(table_name, other_table_name, foreign_key_options)
end

Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/migration/references_foreign_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase
@connection.drop_table "testing", if_exists: true
end
end

test "multiple foreign keys can be added to the same table" do
@connection.create_table :testings do |t|
t.integer :col_1
t.integer :col_2

t.foreign_key :testing_parents, column: :col_1
t.foreign_key :testing_parents, column: :col_2
end

fks = @connection.foreign_keys("testings").sort_by(&:column)

fk_definitions = fks.map {|fk| [fk.from_table, fk.to_table, fk.column] }
assert_equal([["testings", "testing_parents", "col_1"],
["testings", "testing_parents", "col_2"]], fk_definitions)
end
end
end
end
Expand Down