首页 > 解决方案 > 测试 ActiveRecord 时如何解释不明确的列名?

问题描述

我正在协助 gem activerecord-sqlserver-adapter完成它对 Rails 5.2.3 的测试。我试图了解下面(和这里)的 ActiveRecord 测试。

# File activerecord/test/cases/relations_test.rb
def test_pluck_with_from_includes_quoted_original_table_name
  relation = Comment.joins(:post).order(:id)
  subquery = Comment.from("#{Comment.quoted_table_name} /*! USE INDEX (PRIMARY) */").joins(:post).order(:id)
  assert_equal relation.pluck(:id), subquery.pluck(:id)
end

当前运行测试时,出现以下错误。

Error:
RelationTest#test_pluck_with_from_includes_quoted_original_table_name_coerced:
ActiveRecord::StatementInvalid: TinyTds::Error: Ambiguous column name 'id'.: SELECT [id] FROM [comments] /*! USE INDEX (PRIMARY) */ LEFT OUTER JOIN [posts] ON [posts].[id] = [comments].[post_id]  ORDER BY [id] ASC

bin/rails test mnt/c/Users/mcdonaldd/Documents/open_source/sqlserver-main/test/cases/coerced_tests.rb:763

据我所知,我希望这是因为我没有指定“id”列应该来自哪个表......但是我猜测构建通过测试的解决方案不会将其更改为......

assert_equal relation.pluck(:id), subquery.pluck('posts.id')

任何人都可以让我了解文档中原始形式的上述行在这种情况下应该如何在不传递特定表名的情况下工作?

标签: ruby-on-railssql-serveractiverecord

解决方案


推荐阅读