开发者

rails 3 test error:Mysql2::Error: Unknown column

开发者 https://www.devze.com 2023-03-09 05:47 出处:网络
guys,I get an error when I test my rails app following the http://guides.rubyonrails.org/testing.html,It said :

guys,I get an error when I test my rails app following the http://guides.rubyonrails.org/testing.html,It said :

test_the_truth(ClassroomTypeTest): ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'type' in 'field list': INSERT INTO classroom_types (type, memo, created_at, updated_at, id) VALUES ('MyString', 'MyString', '2011-05-31 08:40:54', '2011-05-31 08:40:54', 980190962)

I am confused, I do not have a column in my classroom_types table,why will it appear such an error? Can anyone help me? Many Thanks!

Here are my models and the relationship:

The ClassroomType model:

class ClassroomType < ActiveRecord::Base
  has_many :classrooms
end

The Classroom model:

class Classroom < ActiveRecord::Base
   belongs_to :classroom_type
   belongs_to :teaching_building
end

The TeachingBuilding model:

class TeachingBuilding < ActiveRecord::Base
  has_many :classrooms
end

And here are the corresponding tables:

classrooms:

  • id
  • classroom_no
  • classroom_name
  • classroom_type_id
  • teaching_building_id

classroom_types:

  • id
  • name
  • memo

teaching_buildings:


I would guess that the problem lies with your column naming. When rails does polymorphic associations, it joins the tables with an id and selects the joined table from a xxx_type column. I could imagine that your problem goes away, if you replace "type" with (for example) "kind" within your database names and column names.


Samuel

I had encountered a similar problem but solved here's how

  1. go to mysql localserver
  2. then change the mysql column name correspoding to model having belongs_to
  3. Use "ALTER TABLE tablename CHANGE oldname newname VARCHAR (10) ;"
  4. This is because rails creates default as user_id


I think problem is column naming, check your database table column name and its type and set it according to your error message said

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号