{"action":"closed","issue":{"url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/417","labels_url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/417/labels{/name}","comments_url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/417/comments","events_url":"https://api.github.com/repos/rails-sqlserver/activerecord-sqlserver-adapter/issues/417/events","html_url":"https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/417","id":108952286,"number":417,"title":"Problem when order by field name is a T-SQL keyword","user":{"login":"paulewetzel","id":896811,"avatar_url":"https://avatars.githubusercontent.com/u/896811?v=3","gravatar_id":"","url":"https://api.github.com/users/paulewetzel","html_url":"https://github.com/paulewetzel","followers_url":"https://api.github.com/users/paulewetzel/followers","following_url":"https://api.github.com/users/paulewetzel/following{/other_user}","gists_url":"https://api.github.com/users/paulewetzel/gists{/gist_id}","starred_url":"https://api.github.com/users/paulewetzel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulewetzel/subscriptions","organizations_url":"https://api.github.com/users/paulewetzel/orgs","repos_url":"https://api.github.com/users/paulewetzel/repos","events_url":"https://api.github.com/users/paulewetzel/events{/privacy}","received_events_url":"https://api.github.com/users/paulewetzel/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-29T20:21:43Z","updated_at":"2015-10-18T22:21:08Z","closed_at":"2015-10-18T22:21:08Z","body":"The following SQL\r\n\r\nEXEC sp_executesql N'SELECT [phrasing_phrases].* FROM [phrasing_phrases] WHERE [phrasing_phrases].[locale] = ''en'' AND (value is not null)  ORDER BY phrasing_phrases.key\r\n\r\nerrors out because phrasing_phrases.key needs to be formatted as phrasing_phrases.[key] in order for it to work, as \"key\" is a T-SQL keyword\r\n\r\nI experimented with formatting  [ phrasing_phrases].[key] in visitors/sqlserver.visit_Orders_And_Let_Fetch_Happen, but that caused an error in Arel. Also note that  [phrasing_phrases].[locale] = ''en'' is actually single quoted twice and not double quoted as it look here.\r\n\r\nHere is a relevant chunk of the trace:\r\n\r\nTinyTds::Error: Incorrect syntax near the keyword 'key'.: EXEC sp_executesql N'SELECT [phrasing_phrases].* FROM [phrasing_phrases] WHERE [phrasing_phrases].[locale] = ''en'' AND (value is not null)  ORDER BY phrasing_phrases.key'\r\nCompleted 500 Internal Server Error in 31ms\r\n\r\nTinyTds::Error - Incorrect syntax near the keyword 'key'.:\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:340:in `handle_to_names_and_values_dblib'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:329:in `handle_to_names_and_values'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:304:in `_raw_select'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:299:in `block in raw_select'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'\r\n  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:299:in `raw_select'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:244:in `sp_executesql'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:19:in `exec_query'\r\n  activerecord-sqlserver-adapter (4.2.3) lib/active_record/connection_adapters/sqlserver/database_statements.rb:199:in `select'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `block in select_all'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/query_cache.rb:83:in `cache_sql'\r\n  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `select_all'\r\n  activerecord (4.2.0) lib/active_record/querying.rb:39:in `find_by_sql'\r\n  activerecord (4.2.0) lib/active_record/relation.rb:638:in `exec_queries'\r\n  activerecord (4.2.0) lib/active_record/relation.rb:514:in `load'\r\n  activerecord (4.2.0) lib/active_record/relation.rb:243:in `to_a'\r\n  activerecord (4.2.0) lib/active_record/relation/delegation.rb:132:in `method_missing'\r\n  activerecord (4.2.0) lib/active_record/relation/delegation.rb:99:in `method_missing'\r\n  phrasing (3.2.9) app/controllers/phrasing_phrases_controller.rb:22:in `index'\r\n\r\n"}}