Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only default to select primary key when non-composite #47836

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def call(attribute, value)
end

if value.select_values.empty?
value = value.select(value.table[value.klass.primary_key])
if value.klass.composite_primary_key?
raise ArgumentError, "Cannot map composite primary key #{value.klass.primary_key} to #{attribute.name}"
else
value = value.select(value.table[value.klass.primary_key])
end
end

attribute.in(value.arel)
Expand Down
18 changes: 17 additions & 1 deletion activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
require "models/categorization"
require "models/edge"
require "models/subscriber"
require "models/cpk"

class RelationTest < ActiveRecord::TestCase
fixtures :authors, :author_addresses, :topics, :entrants, :developers, :people, :companies, :developers_projects, :accounts, :categories, :categorizations, :categories_posts, :posts, :comments, :tags, :taggings, :cars, :minivans
fixtures :authors, :author_addresses, :topics, :entrants, :developers, :people, :companies, :developers_projects, :accounts, :categories, :categorizations, :categories_posts, :posts, :comments, :tags, :taggings, :cars, :minivans, :cpk_orders

def test_do_not_double_quote_string_id
van = Minivan.last
Expand Down Expand Up @@ -945,6 +946,21 @@ def test_find_all_using_where_with_relation_and_alternate_primary_key
}
end

def test_find_all_using_where_with_relation_with_no_selects_and_composite_primary_key_raises
order = cpk_orders(:cpk_groceries_order_1)
subquery = Cpk::Order.where(Cpk::Order.primary_key => [order.id])

assert_nothing_raised do
Cpk::Order.where(id: subquery.select(:id)).to_a
end

error = assert_raise(ArgumentError) do
Cpk::Order.where(id: subquery).to_a
end

assert_equal "Cannot map composite primary key [\"shop_id\", \"id\"] to id", error.message
end

def test_find_all_using_where_with_relation_does_not_alter_select_values
david = authors(:david)

Expand Down