ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
我正在尝试创建一个已发表评论的唯一患者列表,按照最先发表评论的患者的顺序排列。
这是我的 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: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
: SELECT * FROM (SELECT DISTINCT ON (patient_id) * FROM "comments" WHERE "comments"."clinician_id" = ORDER BY patient_id, created_at DESC) sub ORDER BY created_at desc
我试着按照 24619117 and my output is a combination of this and the answer 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
我对 PostgresSQL 没有经验,部分问题是我在 Rails 上使用 Ruby 来获取 SQL,而且方法并不简单。我一直在使用 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, 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).
可以通过向 database.yml
文件添加指令来完全禁用准备好的语句:
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
我正在尝试创建一个已发表评论的唯一患者列表,按照最先发表评论的患者的顺序排列。
这是我的 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: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1 : SELECT * FROM (SELECT DISTINCT ON (patient_id) * FROM "comments" WHERE "comments"."clinician_id" = ORDER BY patient_id, created_at DESC) sub ORDER BY created_at desc
我试着按照 24619117 and my output is a combination of this and the answer 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
我对 PostgresSQL 没有经验,部分问题是我在 Rails 上使用 Ruby 来获取 SQL,而且方法并不简单。我一直在使用 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, 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).
可以通过向 database.yml
文件添加指令来完全禁用准备好的语句:
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