Skip to content

Commit 8b43c13

Browse files
committed
Use composite primary_key value as a fallback for query_constraints_list
Given a model with a composite primary key, the `query_constraints_list` should equal the `primary_key` value to enable capabilities managed by `query_constraints_list` such as `destroy`, `update`, `delete` and others.
1 parent 62e3614 commit 8b43c13

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def query_constraints(*columns_list)
493493

494494
def query_constraints_list # :nodoc:
495495
@query_constraints_list ||= if base_class? || primary_key != base_class.primary_key
496-
@_query_constraints_list
496+
primary_key.is_a?(Array) ? primary_key : @_query_constraints_list
497497
else
498498
base_class.query_constraints_list
499499
end

activerecord/test/cases/persistence_test.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require "models/ship"
2222
require "models/admin"
2323
require "models/admin/user"
24+
require "models/cpk"
2425

2526
class PersistenceTest < ActiveRecord::TestCase
2627
fixtures :topics, :companies, :developers, :accounts, :minimalistics, :authors, :author_addresses,
@@ -1448,25 +1449,14 @@ def test_query_constraints_list_is_nil_if_primary_key_is_nil
14481449
assert_nil klass.query_constraints_list
14491450
end
14501451

1451-
def test_query_constraints_uses_primary_key_by_default
1452-
post = posts(:welcome)
1453-
assert_uses_query_constraints_on_reload(post, "id")
1452+
def test_query_constraints_list_is_nil_for_non_cpk_model
1453+
assert_nil Post.query_constraints_list
1454+
assert_nil Dashboard.query_constraints_list
14541455
end
14551456

1456-
def test_query_constraints_uses_manually_configured_primary_key
1457-
dashboard = dashboards(:cool_first)
1458-
assert_uses_query_constraints_on_reload(dashboard, "dashboard_id")
1459-
end
1460-
1461-
def test_child_overriden_primary_key_is_used_as_query_constraint
1462-
topic = topics(:first)
1463-
assert_uses_query_constraints_on_reload(topic, "id")
1464-
1465-
title_pk_topic = topic.becomes(TitlePrimaryKeyTopic)
1466-
title_pk_topic.author_name = "Nikita"
1467-
1468-
sql = capture_sql { title_pk_topic.save }.first
1469-
assert_match(/WHERE .*title/, sql)
1457+
def test_query_constraints_list_equals_to_composite_primary_key
1458+
assert_equal(["shop_id", "id"], Cpk::Order.query_constraints_list)
1459+
assert_equal(["author_id", "number"], Cpk::Book.query_constraints_list)
14701460
end
14711461

14721462
def test_child_keeps_parents_query_constraints
@@ -1477,6 +1467,10 @@ def test_child_keeps_parents_query_constraints
14771467
assert_uses_query_constraints_on_reload(used_clothing_item, ["clothing_type", "color"])
14781468
end
14791469

1470+
def test_child_keeps_parents_query_contraints_derived_from_composite_pk
1471+
assert_equal(["author_id", "number"], Cpk::BestSeller.query_constraints_list)
1472+
end
1473+
14801474
def assert_uses_query_constraints_on_reload(object, columns)
14811475
flunk("columns argument must not be empty") if columns.blank?
14821476

activerecord/test/models/cpk/book.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ class Book < ActiveRecord::Base
55
self.table_name = :cpk_books
66
self.primary_key = [:author_id, :number]
77
end
8+
9+
class BestSeller < Book
10+
end
811
end

0 commit comments

Comments
 (0)