mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #23614 from georgemillo/foreign_key
Let t.foreign_key use the same `to_table` twice Conflicts: activerecord/CHANGELOG.md
This commit is contained in:
commit
84c18246bc
3 changed files with 23 additions and 2 deletions
|
@ -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 a regression on has many association, where calling a child from parent in child's callback
|
||||
results in same child records getting added repeatedly to target.
|
||||
|
||||
|
|
|
@ -212,7 +212,7 @@ module ActiveRecord
|
|||
def initialize(name, temporary, options, as = nil)
|
||||
@columns_hash = {}
|
||||
@indexes = {}
|
||||
@foreign_keys = {}
|
||||
@foreign_keys = []
|
||||
@primary_keys = nil
|
||||
@temporary = temporary
|
||||
@options = options
|
||||
|
@ -330,7 +330,7 @@ module ActiveRecord
|
|||
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
|
||||
|
|
|
@ -144,6 +144,22 @@ module ActiveRecord
|
|||
@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")
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue