khwangster / activerecord4-redshift-adapter

ActiveRecord 4 DB adapter for Amazon Redshift
MIT License
3 stars 4 forks source link

Undefined method `type' for "character varying(256)":String #1

Closed bramswenson closed 10 years ago

bramswenson commented 10 years ago

Hi, thanks for all your work on this adapter. :+1: With it I am able to connect to redshift and run some simple queries. However, other simple queries fail. I have a table with nothing but varchar columns:

psql (9.3.5, server 8.0.2)
SSL connection (cipher: ECDHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

production=> \d users
                   Column                    |          Type          | Modifiers 
-------------------------------------------+------------------------+-----------
 education                                   | character varying(256) | 
 occupation                                  | character varying(256) | 
 state                                       | character varying(256) | 
...

If I try to query these columns I get an error that seems to indicate that the column type isn't recognized properly:

[3] pry(main)> User.count
D, [2014-10-24T10:29:44.499199 #11922] DEBUG -- :    (609.7ms)  SELECT COUNT(*) FROM "users"
=> 20000000
[5] pry(main)> User.where(state: 'FL').limit(1).first
NoMethodError: undefined method `type' for "character varying(256)":String
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/column.rb:18:in `type'

Any ideas? I'm not familiar with AR internals regarding type casting. I'm going to dig in the next chance I get, but maybe you have some idea since you wrote it.

And here is the full stack trace:

/home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/column.rb:18:in `type': undefined method `type' for "character varying(256)":String (NoMethodError)
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/bundler/gems/activerecord4-redshift-adapter-028cf947a713/lib/active_record/connection_adapters/redshift/quoting.rb:21:in `quote'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract_adapter.rb:118:in `block in compile'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract_adapter.rb:118:in `map'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract_adapter.rb:118:in `compile'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `to_sql'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/querying.rb:39:in `find_by_sql'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation.rb:635:in `exec_queries'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation.rb:511:in `load'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation.rb:241:in `to_a'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation/finder_methods.rb:500:in `find_nth_with_limit'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation/finder_methods.rb:488:in `find_nth'
from /home/bram/Code/redshift/.bundle/bundle/ruby/2.1.0/gems/activerecord-4.2.0.beta2/lib/active_record/relation/finder_methods.rb:134:in `first'
from lib/models.rb:21:in `<main>'
khwangster commented 10 years ago

I couldn't reproduce this issue querying against a character varying column. I'll have to dig into the code later to see what is actually happening.

Just curious, what OS and ruby version are you using?

bramswenson commented 10 years ago
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
$ uname -a
Linux bram 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ cat Gemfile
source 'https://rubygems.org'

gem 'dotenv'
gem 'pg'
gem 's3'
gem 'pry'
gem 'rake',     '~> 10.3.2'
gem 'whenever', '~> 0.9.3', require: false
gem 'activerecord', '4.2.0.beta.2'
gem 'activerecord4-redshift-adapter',
  github: 'khwangster/activerecord4-redshift-adapter',
  ref: '4.0'

group :development do
  gem 'capistrano',               '~> 3.2.1'
  gem 'capistrano-bundler',       '~> 1.1.3'
  gem 'capistrano-rbenv',         '~> 2.0.2'
  gem 'capistrano-rbenv-install', '~> 1.0.0'
end
khwangster commented 10 years ago

Ah, it seems like you're referencing the 4.0 tag. That reference is for compatibility with ActiveRecord 4.0. I created a 4.2 tag for ActiveRecord 4.2 support (or just use master). Let me know if that solves your problem.

bramswenson commented 10 years ago

Perfect. That works. Thanks @khwangster!