ProtocolViolation:错误:绑定消息提供 0 个参数,但准备好的语句“”需要 1 个参数

2024-03-22

我正在尝试创建一个留下评论的独特患者列表,按照最先留下最近评论的患者的顺序排列。

这是我用于创建列表的 Ruby .erb 代码:

@comment_list.order("created_at desc").each_with_index do |comment, index|

@comment_list 在控制器中定义为:

  @comments = current_clinician.comments.select('ON (patient_id) *').uniq
  @comments = @comments.order("patient_id, created_at DESC")
  @comment_list = Comment.select('*').from("(#{@comments.to_sql}) sub")

我收到一条 ActiveRecord::StatementInvalid 消息:

PG::ProtocolViolation: 错误: 绑定消息提供 0 个参数,但准备好的语句 "" 需要 1 个参数 : SELECT * FROM (SELECT DISTINCT ON (patent_id) * FROM "comments" WHERE "comments"."clinician_id" = $1 ORDER BY Patient_id,created_at DESC) sub ORDER BYcreated_at desc

我尝试遵循以下答案24619117 https://stackoverflow.com/questions/24619117/select-all-threads-and-order-by-the-latest-one/24623210#24623210我的输出是这个和答案的组合29660396 https://stackoverflow.com/questions/29660396/groupingerror-error-column-must-appear-in-the-group-by-clause-or-be-used-i?answertab=votes#tab-top.

$ rails -v
Rails 4.1.8
$ ruby -v
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
$ psql --version
psql (PostgreSQL) 9.4.1

我对 PostgresQL 缺乏经验,部分问题是我使用 Ruby on Rails 来获取 SQL,而且方法并不简单。我一直在使用http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-from http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-from

请提出建议


在你的情况下,似乎因为你正在使用@comments.to_sql您将该准备好的语句拉入您的子选择中,而不为其引入参数。您可以尝试仅包含评论数据,如下所示:

  @comments = current_clinician.comments.select('ON (patient_id) *').uniq.order("patient_id, created_at DESC").include(:comment)
  @comment_list = @comments.include(:comment)

这个问题似乎也来自 Rails 中准备好的语句的构建方式,并且可能是由 Rails 本身内部的任一问题引起的(Rails 问题)#15920 https://github.com/rails/rails/issues/15920,已在 Rails 4.2 中修复)或通过帮助生成查询的各种 gem 的问题(例如:Rails 问题#20236 https://github.com/rails/rails/issues/20236)或者甚至是你定义模型关联的方式(Rails issues#12852 https://github.com/rails/rails/issues/12852).

可以通过向您的程序添加指令来彻底禁用准备好的语句database.yml file:

production:
  adapter: postgresql
  database: prod_dbname
  username: prod_user
  password: prod_pass
  prepared_statements: false

但首先,您可能需要检查并确保没有在模型关联中使用不必要的参数,如下所示:

class DashboardTab < ActiveRecord::Base
  has_many :dashboard_tab_feeds, foreign_key: :dashboard_tab_id, dependent: :destroy
  has_many :social_feeds, through: :dashboard_tab_feeds
end

class DashboardTabFeed < ActiveRecord::Base
  belongs_to :social_feed
  belongs_to :dashboard_tab
end

class SocialFeed < ActiveRecord::Base
  has_many :dashboard_tab_feeds, foreign_key: :social_feed_id, dependent: :destroy
end

...应该忽略掉foreign_key, 像这样:

class DashboardTab < ActiveRecord::Base
  has_many :dashboard_tab_feeds, dependent: :destroy
  has_many :social_feeds, through: :dashboard_tab_feeds
end

class DashboardTabFeed < ActiveRecord::Base
  belongs_to :social_feed
  belongs_to :dashboard_tab
end

class SocialFeed < ActiveRecord::Base
  has_many :dashboard_tab_feeds, dependent: :destroy
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ProtocolViolation:错误:绑定消息提供 0 个参数,但准备好的语句“”需要 1 个参数 的相关文章

随机推荐