rails 3 test error:Mysql2::Error: Unknown column
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:
- 开发者_开发知识库id
- t_no
- name
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
- go to mysql localserver
- then change the mysql column name correspoding to model having belongs_to
- Use
"ALTER TABLE tablename CHANGE oldname newname VARCHAR (10) ;"
- 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
精彩评论