From a48b675d54101b048228d1011ffa426c2b7fe94d Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 10 Jun 2014 12:09:58 +0200 Subject: fk: `:primary_key` option for non-standard pk's. --- .../test/cases/migration/foreign_key_test.rb | 20 ++++++++++++++++++++ activerecord/test/support/ddl_helper.rb | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'activerecord/test') 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 -- cgit v1.2.3