如何停止这些PG :: ProtocolViolation错误?

时间:2014-01-21 11:34:44

标签: ruby-on-rails postgresql

我的方法是:

class Survey < ActiveRecord::Base
  def create_matching_batteries
    unless inactive?
      update_column :battery_id, nil unless battery
      @battery = Battery.where(:review_id => review.id, :question_id => question.id).first_or_create
      @battery.surveys << self
    end
  end
end

当我运行@survey.create_matching_batteries时,我明白了:

Survey Load (5.2ms)  SELECT "surveys".* FROM "surveys" WHERE "surveys"."competitor_id" = $1 AND "surveys"."id" = $1 ORDER BY "surveys"."id" ASC LIMIT 1  [["competitor_id", 248], ["id", 15183]]
D, [2014-01-21T22:28:08.830446 #3392] DEBUG -- :   Survey Load (5.2ms)  SELECT "surveys".* FROM "surveys" WHERE "surveys"."competitor_id" = $1 AND "surveys"."id" = $1 ORDER BY "surveys"."id" ASC LIMIT 1  [["competitor_id", 248], ["id", 15183]]
PG::ProtocolViolation: ERROR:  bind message supplies 2 parameters, but prepared statement "a15" requires 1
: SELECT  "surveys".* FROM "surveys"  WHERE "surveys"."competitor_id" = $1 AND "surveys"."id" = $1  ORDER BY "surveys"."id" ASC LIMIT 1
E, [2014-01-21T22:28:08.830528 #3392] ERROR -- : PG::ProtocolViolation: ERROR:  bind message supplies 2 parameters, but prepared statement "a15" requires 1
: SELECT  "surveys".* FROM "surveys"  WHERE "surveys"."competitor_id" = $1 AND "surveys"."id" = $1  ORDER BY "surveys"."id" ASC LIMIT 1
   (0.3ms)  ROLLBACK
D, [2014-01-21T22:28:08.833655 #3392] DEBUG -- :    (0.3ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR:  bind message supplies 2 parameters, but prepared statement "a15" requires 1
: SELECT  "surveys".* FROM "surveys"  WHERE "surveys"."competitor_id" = $1 AND "surveys"."id" = $1  ORDER BY "surveys"."id" ASC LIMIT 1
from /Users/steven/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql_adapter.rb:786:in `get_last_result'

在“Railspeak”而不是“Postgrespeak”中,ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR: bind message supplies 2 parameters, but prepared statement "a15" requires 1是什么意思?它如何帮助我调试我的方法?

我的环境:

$ rails -v
Rails 4.0.2
$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]
$ psql --version
psql (PostgreSQL) 9.3.1

3 个答案:

答案 0 :(得分:1)

禁用准备好的语句

production:
  adapter: postgresql
  prepared_statements: false

结帐http://edgeguides.rubyonrails.org/configuring.html

答案 1 :(得分:0)

我遇到了同样的问题,但是在对代码进行了反检查之后,我发现我在两个不同的字段中使用了相同的变量,即我分别写了fieldA=$2fieldB=$2。更正后,一切正常。

答案 2 :(得分:0)

我的问题是我在变量周围使用了引号

var userSources = firestore.collection('sources');
    userSources.get().then(snapshot => {
        snapshot.forEach(doc => {
            var source = doc.data();
            sources.push(source);
        });
    return sources;

    }).then((sources) => {
        // 2: Connect to Plaid and query accounts
        var promises = [];
        sources.forEach(function(source) {
            promises.push({
                id: source.id,
                p: client.getBalance(source.access_token)
            });
        });
        return Promise.all(promises);

    }).then...
    ```

我删除了这些引号

quiz_subject = subject_id WHERE subject_name = '$1';