1
votes

Using Sqlite3 with Rails3 and I receive "ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column" with a has_and_belongs_to_many situation. From rails console connected to Sqlite3, errors when page.editors or page.admin_users

[[[EDIT: Solved because of typos in the join table id's]]]

gems installed

rails 3.0.9

sqlite3 1.3.3 x86-mingw32

sqlite3-ruby

(win7 64bit dev machine)

page.rb --> OK





> class Page      
>        belongs_to :subject
>        has_many :sections
>        has_and_belongs_to_many :editors, :class_name => "AdminUser"
>        #has_and_belongs_to_many :admin_users
>        
>        end

admin_users.rb --> OK



>  class AdminUser      
>       has_and_belongs_to_many :pages
>       scope :named, lambda {|first,last| where(:first_name =>
> first, :last_name => last)}
>       
>     end

migration file --> Spot the Typos!

    class CreateAdminUsersPagesJoin  false do |t|
          t.integer :admin_users_id, :foreign_key => true  # Should be admin_user_id
          t.integer :page_id, :foreign_key => true
        end
        add_index :admin_users_pages, [:admin_users_id, :page_id]  # Again admin_user_id
      end

      def self.down  
        drop_table :admin_users_pages
      end

    end

rails console errors

irb(main):004:0> page.admin_users
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: admin_users_pages.admin_user_id: SELECT * FROM "admin_users
" INNER JOIN "admin_users_pages" ON "admin_users".id = "admin_users_pages".admin_user_id WHERE ("admin_users_pages".page_id = 2 )

Thanks to Heikki for the posts. I fixed it myself before looking back here only now (d'oh) but I'll happily accept the answer. Post it and I'll check it since that was correct. Cheers.

1
Aren't you missing def self.up from your migration? - Heikki
:admin_users_id should probably :admin_user_id. - Heikki
Thanks for the comments. The def self.up is there so I think the stackoverflow missed the paste?? Anyway, I had not only the one typo but two. I went to sqlite3 and hit .schema admin_users_pages and I saw that the foreign keys were plural so that confirms one comment. Also note the index is also incorrect as plural. That I think fixed it. I made sure sqlite3-ruby was in the Gemfile for the hell of it and ran a bundle update. Solved it myself but I'll rep Heikki here since that was the issue. Heikki, I'll update my post and you post your answer and I'll check it. Cheers - Walter Spicer

1 Answers