Skip to content

Commit f42d9cb

Browse files
committed
[Fix rails#51164] Model.query_constraints with single non-primary-key column raises incorrect error
1 parent 649e99b commit f42d9cb

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Using `Model.query_constraints` with a single non-primary-key column used to raise as expected, but with an
2+
incorrect error message. This has been fixed to raise with a more appropriate error message.
3+
4+
*Joshua Young*
5+
16
* Fix `has_one` association autosave setting the foreign key attribute when it is unchanged.
27

38
This behaviour is also inconsistent with autosaving `belongs_to` and can have unintended side effects like raising

activerecord/lib/active_record/reflection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def derive_fk_query_constraints(foreign_key)
787787
primary_query_constraints = active_record.query_constraints_list
788788
owner_pk = active_record.primary_key
789789

790-
if primary_query_constraints.size != 2
790+
if primary_query_constraints.size > 2
791791
raise ArgumentError, <<~MSG.squish
792792
The query constraints list on the `#{active_record}` model has more than 2
793793
attributes. Active Record is unable to derive the query constraints

activerecord/test/cases/associations_test.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,29 @@ def test_assign_composite_foreign_key_belongs_to_association
368368
assert_equal(another_blog.id, comment.blog_id)
369369
end
370370

371-
def test_query_constraints_that_dont_include_the_primary_key_raise
371+
def test_query_constraints_that_dont_include_the_primary_key_raise_with_a_single_column
372+
original = Sharded::BlogPost.instance_variable_get(:@query_constraints_list)
373+
Sharded::BlogPost.query_constraints :title
374+
Sharded::BlogPost.has_many :comments_without_single_column_query_constraints, primary_key: [:blog_id, :id], class_name: "Comment"
375+
blog_post = sharded_blog_posts(:great_post_blog_one)
376+
377+
error = assert_raises ArgumentError do
378+
blog_post.comments_without_single_column_query_constraints.to_a
379+
end
380+
381+
assert_equal "The query constraints on the `Sharded::BlogPost` model does not include the primary key so Active Record is unable to derive the foreign key constraints for the association. You need to explicitly define the query constraints for this association.", error.message
382+
ensure
383+
Sharded::BlogPost.instance_variable_set(:@query_constraints_list, original)
384+
end
385+
386+
def test_query_constraints_that_dont_include_the_primary_key_raise_with_multiple_columns
372387
original = Sharded::BlogPost.instance_variable_get(:@query_constraints_list)
373388
Sharded::BlogPost.query_constraints :title, :revision
374-
Sharded::BlogPost.has_many :comments_without_query_constraints, primary_key: [:blog_id, :id], class_name: "Comment"
389+
Sharded::BlogPost.has_many :comments_without_multiple_column_query_constraints, primary_key: [:blog_id, :id], class_name: "Comment"
375390
blog_post = sharded_blog_posts(:great_post_blog_one)
376391

377392
error = assert_raises ArgumentError do
378-
blog_post.comments_without_query_constraints.to_a
393+
blog_post.comments_without_multiple_column_query_constraints.to_a
379394
end
380395

381396
assert_equal "The query constraints on the `Sharded::BlogPost` model does not include the primary key so Active Record is unable to derive the foreign key constraints for the association. You need to explicitly define the query constraints for this association.", error.message

0 commit comments

Comments
 (0)