From a48b675d54101b048228d1011ffa426c2b7fe94d Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 10 Jun 2014 12:09:58 +0200 Subject: [PATCH] fk: `:primary_key` option for non-standard pk's. --- .../abstract/schema_statements.rb | 4 +++- .../test/cases/migration/foreign_key_test.rb | 20 +++++++++++++++++++ activerecord/test/support/ddl_helper.rb | 4 ++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index da6b15dad0..db04ebc802 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -647,9 +647,11 @@ module ActiveRecord end def add_foreign_key(from_table, to_table, options = {}) + primary_key = options.fetch(:primary_key, "id") + options = { column: options.fetch(:column), - primary_key: "id", + primary_key: primary_key, name: foreign_key_name(from_table, options) } at = create_alter_table from_table 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