提交 aedde2a3 编写于 作者: G George Millo

Let t.foreign_key use the same `to_table` twice

Previously if you used `t.foreign_key` twice within the same
`create_table` block using the same `to_table`, all statements except
the final one would fail silently. For example, the following code:

    def change
      create_table :flights do |t|
        t.integer :from_id, index: true, null: false
        t.integer :to_id,   index: true, null: false

        t.foreign_key :airports, column: :from_id
        t.foreign_key :airports, column: :to_id
      end
    end

Would only create one foreign key, on the column `from_id`.

This commit allows multiple foreign keys to the same table to be created
within one `create_table` block.
上级 156c2cb5
* 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*
* Rework `ActiveRecord::Relation#last`
1. Never perform additional SQL on loaded relation
......
......@@ -212,7 +212,7 @@ class TableDefinition
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 @@ 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
......
......@@ -144,6 +144,26 @@ 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")
assert_equal fks.length, 2
assert_equal "testings", fks[0].from_table
assert_equal "testing_parents", fks[0].to_table
assert_equal "col_1", fks[0].column
assert_equal "testings", fks[1].from_table
assert_equal "testing_parents", fks[1].to_table
assert_equal "col_2", fks[1].column
end
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册