ProtocolViolation:错误:绑定消息用品0参数,但prepared声明""要求1 [英] ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1

查看:809
本文介绍了ProtocolViolation:错误:绑定消息用品0参数,但prepared声明""要求1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建独特的病人谁留下的评论列表,以便病人谁离开了最新评论第一。

I am trying to create a list of unique patients who have left comments, in order of patient who left the most recent comment first.

这是我的红宝石.erb code创建列表:

This is my Ruby .erb code to create the list:

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

@comment_list在控制器定义:

@comment_list is defined in the controller as:

  @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消息:

I get an ActiveRecord::StatementInvalid message:

PG :: ProtocolViolation:错误:绑定消息用品0参数,但prepared声明,要求1   :SELECT * FROM(SELECT DISTINCT ON(patient_id)* FROM意见WHERE意见clinician_id= $ 1目BY patient_id,created_at DESC)子ORDER BY created_at递减

PG::ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1 : SELECT * FROM (SELECT DISTINCT ON (patient_id) * FROM "comments" WHERE "comments"."clinician_id" = $1 ORDER BY patient_id, created_at DESC) sub ORDER BY created_at desc

我试图遵循<一个答案href="http://stackoverflow.com/questions/24619117/select-all-threads-and-order-by-the-latest-one/24623210#24623210">24619117我的输出是这一点,并回答上面<一组合href="http://stackoverflow.com/questions/29660396/groupingerror-error-column-must-appear-in-the-group-by-clause-or-be-used-i?answertab=votes#tab-top">29660396.

I tried to follow the answer on 24619117 and my output is a combination of this and the answer top 29660396.

$ 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

建议请

推荐答案

在你的情况,似乎是因为你使用的是 @ comments.to_sql 你是拉了prepared语句插入子选择没有带来它的参数。你可以尝试只包括像这样的注释数据:

In your case it seems that because you are using the @comments.to_sql you are pulling that prepared statement into your subselect without bringing in the parameter for it. You can try just including the comment data like this:

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

这个问题似乎还来自于这样的prepared是建立在Rails的声明,并可能通过在自己的Rails(Rails的问题的#15920 ,它已被固定在滑轨4.2)或用各种宝石的问题,帮助生成查询(例如:Rails的问题的#20236 ),或者甚至可以定义你的模型协会(顺便Rails的问题的#12852 )。

This issue also seems to come from the way that the prepared statements are built in Rails and could be caused by either issues within Rails itself (Rails issue #15920, which has been fixed in Rails 4.2) or by issues with various gems that help to generate queries (example: Rails issue #20236) or even by the way you define your model associations (Rails issues #12852).

有可能只是彻底禁用$ P $加指令,你的的database.yml 文件ppared语句:

It is possible to just outright disable prepared statements by adding a directive to your database.yml file:

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

但首先,你可能要检查,确保你不使用你的模型关联不必要的参数是这样的:

But first, you might want to check and make sure you aren't using unnecessary parameters in your model associations like this:

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

这篇关于ProtocolViolation:错误:绑定消息用品0参数,但prepared声明&QUOT;&QUOT;要求1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆