aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-06-10 12:09:58 +0200
committerYves Senn <yves.senn@gmail.com>2014-06-26 22:03:48 +0200
commita48b675d54101b048228d1011ffa426c2b7fe94d (patch)
tree94737f11dfa1a6f1d28ff7a22f78635ccbf89312 /activerecord/test
parent1c170fdea2be04691c7daa8266084766fe963fff (diff)
downloadrails-a48b675d54101b048228d1011ffa426c2b7fe94d.tar.gz
rails-a48b675d54101b048228d1011ffa426c2b7fe94d.tar.bz2
rails-a48b675d54101b048228d1011ffa426c2b7fe94d.zip
fk: `:primary_key` option for non-standard pk's.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/migration/foreign_key_test.rb20
-rw-r--r--activerecord/test/support/ddl_helper.rb4
2 files changed, 22 insertions, 2 deletions
diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb
index 6ad595668f..2b99ab6ecd 100644
--- a/activerecord/test/cases/migration/foreign_key_test.rb
+++ b/activerecord/test/cases/migration/foreign_key_test.rb
@@ -1,9 +1,12 @@
require 'cases/helper'
+require 'support/ddl_helper'
if ActiveRecord::Base.connection.supports_foreign_keys?
module ActiveRecord
class Migration
class ForeignKeyTest < ActiveRecord::TestCase
+ include DdlHelper
+
class Rocket < ActiveRecord::Base
end
@@ -55,6 +58,23 @@ module ActiveRecord
assert_equal "astronauts_rocket_id_fk", fk.name
end
+ def test_add_foreign_key_with_non_standard_primary_key
+ with_example_table @connection, "space_shuttles", "pk integer PRIMARY KEY" do
+ @connection.add_foreign_key(:astronauts, :space_shuttles,
+ column: "rocket_id", primary_key: "pk", name: "custom_pk")
+
+ foreign_keys = @connection.foreign_keys("astronauts")
+ assert_equal 1, foreign_keys.size
+
+ fk = foreign_keys.first
+ assert_equal "astronauts", fk.from_table
+ assert_equal "space_shuttles", fk.to_table
+ assert_equal "pk", fk.primary_key
+
+ @connection.remove_foreign_key :astronauts, name: "custom_pk"
+ end
+ end
+
def test_remove_foreign_key
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"
diff --git a/activerecord/test/support/ddl_helper.rb b/activerecord/test/support/ddl_helper.rb
index 0107babaaf..43cb235e01 100644
--- a/activerecord/test/support/ddl_helper.rb
+++ b/activerecord/test/support/ddl_helper.rb
@@ -1,8 +1,8 @@
module DdlHelper
def with_example_table(connection, table_name, definition = nil)
- connection.exec_query("CREATE TABLE #{table_name}(#{definition})")
+ connection.execute("CREATE TABLE #{table_name}(#{definition})")
yield
ensure
- connection.exec_query("DROP TABLE #{table_name}")
+ connection.execute("DROP TABLE #{table_name}")
end
end