简体   繁体   中英

Not able to query on “group” in active-record in Rails 3.1.11

I have an application using Ruby on Rails 3.1.11, ruby 1.9.2, mysql 5.6.19. One of my model called "ManagedFile" and it contains a column called "group"

I am able to query eg

ManagedFile.select :name
ManagedFile.select "name"
ManagedFile.select :id
ManagedFile.select :exported, etc
ManagedFile.select('distinct name')
ManagedFile.select('distinct id'), etc

but not ManagedFile.select :group
but not ManagedFile.select "group"
nor ManagedFile.select('distinct group')

it will just throw exception

  1.9.2-head :067 > ManagedFile.select('distinct group')
          ManagedFile Load (0.2ms)  SELECT distinct group FROM `managed_files` 
          Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group FROM `managed_files`' at line 1: SELECT distinct group FROM `managed_files` 
          ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group FROM `managed_files`' at line 1: SELECT distinct group FROM `managed_files` 
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:906:in `prepare'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:906:in `exec_stmt'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:443:in `block in exec_query'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activesupport-3.1.11/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:442:in `exec_query'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:976:in `select'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract/database_statements.rb:20:in `select_all'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/base.rb:470:in `find_by_sql'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/relation.rb:112:in `to_a'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/activerecord-3.1.11/lib/active_record/relation.rb:437:in `inspect'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/railties-3.1.11/lib/rails/commands/console.rb:45:in `start'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/railties-3.1.11/lib/rails/commands/console.rb:8:in `start'
        from /secret_path/.rvm/gems/ruby-1.9.2-head@rails3_1_11/gems/railties-3.1.11/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'1.9.2-head :068 > 

The keyword group is reserved in MySQL. Either rename the column (recommended), or use backticks ( ` ) to escape it.

ManagedFile.select("`group`")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM