rails/rails — Issue #4449: encoding issue with pg adapter and non chars columns
Author: kgiszczak
Created: 2012-01-13 12:17:52+00:00
State: open

I get Encoding::CompatibilityError when I try to run query with non ascii characters on non chars columns.

For example, when I try to run this query:

Post.where('created_at = ?', 'ó')

where Post is:

Post(id: integer, name: string, display_count: integer, created_at: datetime, updated_at: datetime)

I get:

``` ruby
/Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract_adapter.rb:282:in `rescue in log': incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `exec_query'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/postgresql_adapter.rb:1201:in `select'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract/database_statements.rb:16:in `select_all'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/explain.rb:38:in `logging_query_plan'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/relation.rb:170:in `exec_queries'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/relation.rb:159:in `block in to_a'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/explain.rb:31:in `logging_query_plan'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/relation.rb:158:in `to_a'
from /Users/beerkg/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0.rc2/lib/active_record/relation.rb:495:in `inspect'
from script/test.rb:9:in `p'
from script/test.rb:9:in `

'
```

I tested it with ruby 1.9.2, 1.9.3 and ruby-head, and with rails 3.1.3 and 3.2.0rc2
For text or string column types everything works correctly.
I tested it also with mysql2 and sqlite adapters and it works correctly.