Rails3 与 SQLite3::SQLException: 没有这样的列 HABTM
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照惯例,连接表中的外键应该是单数,即。
admin_user_id
而不是admin_users_id
。By convention the foreign keys in the join table should be singular ie.
admin_user_id
instead ofadmin_users_id
.