plentz / lol_dba

lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts.
1.59k stars 69 forks source link

Unitialized Constant On Polymorphic Through Association #42

Closed Undistraction closed 9 years ago

Undistraction commented 9 years ago

First up, here is the dump when I run rake db:find_indexes:

Class: Page
Association type: has_many
Association options: {:through=>:slots, :source=>:slottable, :source_type=>"TextElement", :autosave=>true}
Exception: uninitialized constant Slot::Slottable
/Users/pedr/.gem/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:133:in `compute_type'
/Users/pedr/.gem/ruby/2.0.0/gems/activerecord-4.1.5/lib/active_record/reflection.rb:221:in `klass'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:127:in `block (2 levels) in check_for_indexes'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:92:in `each_pair'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:92:in `block in check_for_indexes'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:88:in `each'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:88:in `check_for_indexes'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/lol_dba.rb:190:in `simple_migration'
/Users/pedr/.gem/ruby/2.0.0/gems/lol_dba-1.6.4/lib/tasks/lol_dba.rake:7:in `block (2 levels) in <top (required)>'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/pedr/.rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
/Users/pedr/.gem/ruby/2.0.0/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
/Users/pedr/.gem/ruby/2.0.0/bin/rake:23:in `load'
/Users/pedr/.gem/ruby/2.0.0/bin/rake:23:in `<main>'

The app is working fine and the relevant setup is:

class Page < ActiveRecord::Base 
   has_many :slots
   has_many :text_elements, through: :slots, source: :slottable, source_type: 'TextElement'
end

class Slot < ActiveRecord::Base
  belongs_to :page
  belongs_to :slottable, polymorphic: true
end
ghost commented 9 years ago

Same issue here.

jvenezia commented 9 years ago

I think this may be related...

Please, create an issue with the following information here https://github.com/plentz/lol_dba/issues:
***************************
Class: Tag
Association type: has_many
Association options: {:through=>:taggings, :source=>:taggable, :source_type=>"Article"}
Exception: uninitialized constant Tagging::Taggable
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/activerecord-4.0.12/lib/active_record/inheritance.rb:129:in `compute_type'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/activerecord-4.0.12/lib/active_record/reflection.rb:178:in `klass'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:127:in `block (2 levels) in check_for_indexes'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:92:in `each_pair'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:92:in `block in check_for_indexes'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:88:in `each'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:88:in `check_for_indexes'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba.rb:190:in `simple_migration'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba/cli.rb:29:in `start'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/lib/lol_dba/cli.rb:17:in `start'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/gems/lol_dba-1.6.4/bin/lol_dba:10:in `<top (required)>'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/bin/lol_dba:23:in `load'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/bin/lol_dba:23:in `<main>'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
/Users/jeremyvenezia/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'
Austio commented 9 years ago

Getting same issue on polymophic on rake with similar exception

plentz commented 9 years ago

@Undistraction @jramos @jvenezia @Austio hey guys, could you try v1.6.6 and see if the bug still happens?

plentz commented 9 years ago

and btw, I will be more then happy to fix this asap if someone can help me build a test case for that(just replicate your namespaced problem here https://github.com/plentz/lol_dba/tree/master/spec/fixtures and create a pull request with the failing bug).

Austio commented 9 years ago

Just ran against 1.6.6 and am getting this on alot of my tests now.

Class: Network Association type: has_many Association options: {:through=>:contacts} Exception: undefined method `klass' for nil:NilClass

plentz commented 9 years ago

@Austio what version of rails you're using? can you send a reduced model that reproduces your problem? I will try to fix this asap :)

Austio commented 9 years ago

No problem, wanted to add more data here. The models are pretty complex in mine and have alot of module stuff.


Class: Post Association type: has_many Association options: {:class_name=>"::SupportCategory", :through=>:categories_posts, :source=>:categories} Exception: undefined method klass' for nil:NilClass /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:117:inblock (2 levels) in check_for_indexes' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:90:in each_pair' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:90:inblock in check_for_indexes' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:86:in each' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:86:incheck_for_indexes' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba.rb:187:in simple_migration' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba/cli.rb:29:instart' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/lib/lol_dba/cli.rb:17:in start' /home/talos/.rvm/gems/ruby-2.0.0-p481/gems/lol_dba-1.6.6/bin/lol_dba:10:in<top (required)>' /home/talos/.rvm/gems/ruby-2.0.0-p481/bin/lol_dba:23:in load' /home/talos/.rvm/gems/ruby-2.0.0-p481/bin/lol_dba:23:in

' /home/talos/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in eval' /home/talos/.rvm/gems/ruby-2.0.0-p481/bin/ruby_executable_hooks:15:in
'

Austio commented 9 years ago

rails is 4.1.4

Austio commented 9 years ago

This was the simplest case i could find.

class Network
 has_many :files,     through: :contacts

class Network
  class Contact #this is nested
    has_many :files,   foreign_key: :network_contact_id
plentz commented 9 years ago

@Austio thanks a lot. I will try to look at it today. I will add tests to be sure that we don't break it again in the future :)

plentz commented 9 years ago

@Undistraction @jramos @jvenezia @Austio hey guys, test it again using 1.6.7. I've fixed a few issues related with older rails versions :)

Austio commented 9 years ago

@plentz was able to run this on 1.6.7

Thanks so much for you help and this gem.

jvenezia commented 9 years ago

Hi @plentz , thanks for working on this. Unfortunately, I still have the exact same error using 1.6.7. I'm on Rails 4.0.13. It may be related with acts-as-taggable-on gem https://github.com/mbleigh/acts-as-taggable-on which uses polymorphic associations.

jvenezia commented 9 years ago

It's because I needed to create models for acts-as-taggable-on, and wrote associations.

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :articles, through: :taggings, source: :taggable, source_type: 'Article'

  ...
end

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, polymorphic: true

  ...
end

I think this should be ignored, I don't know how. The gem already sets up few indexes, and I don't think I need more:

add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true, using: :btree
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree

...

add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree