diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-12-22 13:10:01 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-12-22 13:47:11 -0700 |
commit | 82afeaf23b0cc190d1ed2c8858a11980c71ac336 (patch) | |
tree | ab7aa8f0cd4f3a168c1cef7fcb8b9b36a89188b9 /activerecord/test/cases/migration | |
parent | 68a6c8ecc41ca2e4bee7c7218e8a4be81bbf7e25 (diff) | |
download | rails-82afeaf23b0cc190d1ed2c8858a11980c71ac336.tar.gz rails-82afeaf23b0cc190d1ed2c8858a11980c71ac336.tar.bz2 rails-82afeaf23b0cc190d1ed2c8858a11980c71ac336.zip |
Add `foreign_key` as an option to `references` for `change_table`
This has the same comments as 9af90ffa00ba35bdee888e3e1ab775ba0bdbe72c,
however it affects the `add_reference` method, and `t.references` in the
context of a `change_table` block.
There is a lot of duplication of code between creating and updating
tables. We should re-evaluate the structure of this code from a high
level so changes like this don't need to be made in two places. (Note to
self)
Diffstat (limited to 'activerecord/test/cases/migration')
-rw-r--r-- | activerecord/test/cases/migration/references_foreign_key_test.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/activerecord/test/cases/migration/references_foreign_key_test.rb b/activerecord/test/cases/migration/references_foreign_key_test.rb index 2fa7584a60..c6f0d50a8d 100644 --- a/activerecord/test/cases/migration/references_foreign_key_test.rb +++ b/activerecord/test/cases/migration/references_foreign_key_test.rb @@ -54,6 +54,49 @@ module ActiveRecord end end end + + test "foreign keys can be created while changing the table" do + @connection.create_table :testings + @connection.change_table :testings do |t| + t.references :testing_parent, foreign_key: true + end + + fk = @connection.foreign_keys("testings").first + assert_equal "testings", fk.from_table + assert_equal "testing_parents", fk.to_table + end + + test "foreign keys are not added by default when changing the table" do + @connection.create_table :testings + @connection.change_table :testings do |t| + t.references :testing_parent + end + + assert_equal [], @connection.foreign_keys("testings") + end + + test "foreign keys accept options when changing the table" do + @connection.change_table :testing_parents do |t| + t.integer :other_id + t.index :other_id, unique: true + end + @connection.create_table :testings + @connection.change_table :testings do |t| + t.references :testing_parent, foreign_key: { primary_key: :other_id } + end + + fk = @connection.foreign_keys("testings").find { |k| k.to_table == "testing_parents" } + assert_equal "other_id", fk.primary_key + end + + test "foreign keys cannot be added to polymorphic relations when changing the table" do + @connection.create_table :testings + @connection.change_table :testings do |t| + assert_raises(ArgumentError) do + t.references :testing_parent, polymorphic: true, foreign_key: true + end + end + end end end end |