ActiveRecord Postgres适配器生成无效的SQL WHERE子句:WHERE(" fieldname")

时间:2014-06-25 05:33:34

标签: ruby-on-rails postgresql activerecord ruby-on-rails-4 rails-activerecord

ActiveRecord 4.1.1正在生成一些特殊的SQL保存postgres窒息:

SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type boolean: "uid"

我非常确定"布尔"这里提到的实际上是WHERE()语法,它期望一个布尔表达式作为参数。

uid被定义为我模型的主键(见下文)......但是为什么它只是在WHERE('uid')执行select_all时(参见stacktrace)? (为什么在保存时执行select_all?)

Rails控制台

p = Project.new(name: "test", description: "test", workflow_id: 1)
=> #<Project name: "test", description: "test", user_id: nil, uid: nil, created_at: nil, updated_at: nil, workflow_id: 1, active: nil>
irb(main):031:0> p.save
   (1.9ms)  BEGIN
  Project Load (5.5ms)  SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type boolean: "uid"
LINE 1: SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
                                                     ^
: SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
   (1.7ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type boolean: "uid"
LINE 1: SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
                                                     ^
: SELECT  "projects".* FROM "projects"  WHERE ('uid') LIMIT 1
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:815:in `async_exec'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:815:in `block in exec_no_cache'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:815:in `exec_no_cache'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:947:in `select'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:31:in `select_all'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:69:in `select_all'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/querying.rb:39:in `find_by_sql'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:603:in `exec_queries'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:487:in `load'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:231:in `to_a'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation/finder_methods.rb:451:in `find_take'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation/finder_methods.rb:98:in `take'
... 41 levels...
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:208:in `transaction'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:268:in `block in save'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:267:in `save'
    from (irb):31
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /app/bin/rails:4:in `require'
    from /app/bin/rails:4:in `<main>

模型

class Project < ActiveRecord::Base

  self.primary_key = "uid"

end

ActiveRecord mixin用于生成ID

module ActiveRecordUIDExtension
  extend ActiveSupport::Concern

    def generate_uid
      return unless self.class.column_names.include?("uid")
      self.id = SecureRandom.random_number(36**10).to_s(36)
      while !self.class.find_by(:uid, self.id).nil? do
       self.id = SecureRandom.random_number(36**10).to_s(36)
      end
  end
end

ActiveRecord::Base.send(:include, ActiveRecordUIDExtension)
ActiveRecord::Base.send(:before_create, :generate_uid)

Gemfile.lock的

activerecord (4.1.1)
  activemodel (= 4.1.1)
  activesupport (= 4.1.1)
  arel (~> 5.0.0)

1 个答案:

答案 0 :(得分:2)

你的mixin没有任何意义。这部分就是导致错误的SQL:

self.class.find_by(:uid, self.id)

由于您正在告诉它,因此产生了无效的SQL。 find_by具有灵活的界面,因此必须进行一些解析以查看您是如何调用它的。当你说:

find_by(a, b)

参数解析可能假设您尝试使用此表单:

find_by('some_sql_snippet', placeholder_valid)

,如本例中的文档:

find_by("published_at < ?", 2.weeks.ago)

然后它可能会to_s第一个参数并扫描它占位符。当你说:

find_by(:uid, self.id)

您最终会被:uid.to_sself.id忽略,因为该字符串中没有占位符。这可以解释为什么你在SQL中看到where ('uid')

修复你的mixin有意义:

self.class.find_by(:uid => self.id)

或更好,切换到exists?以避免构建整个模型实例,只是为了查看数据库中是否有一行:

self.class.where(:uid => self.id).exists?
self.class.exists?(:uid => self.id)