Rails3 与 SQLite3::SQLException: 没有这样的列 HABTM

发布于 2024-11-18 21:47:01 字数 1716 浏览 1 评论 0原文

将 Sqlite3 与 Rails3 一起使用,我收到“ActiveRecord::StatementInvalid: SQLite3::SQLException: no such columns”,并且出现 has_and_belongs_to_many 情况。从连接到 Sqlite3 的 Rails 控制台,当 page.editors 或 page.admin_users

[[[编辑:已解决,因为连接表 id 中的拼写错误]]]

gems 安装了

Rails 3.0.9

sqlite3 1.3.3 x86-mingw32

sqlite3-ruby

( win7 64位开发机)

page.rb -->好的




> 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 -->确定


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

迁移文件-->发现错别字!

    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 控制台错误

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 )

感谢 Heikki 的帖子。我自己修复了它,然后才回到这里(d'oh),但我很乐意接受答案。发布它,我会检查它,因为这是正确的。干杯。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文