[英]Rails3 Mysql2::Error: Unknown column - ActiveRecord::StatementInvalid
I'm new to working with databases in Rails at this level, and I've looked for several hours but haven't found a solution to this specific problem. 我刚接触在这个级别的Rails中使用数据库,我已经找了好几个小时但没有找到解决这个特定问题的方法。
Versions: Rails 3.2.9, Ruby 1.9.3, MySQL 5.5.28 (mysql2 gem 2.9.0), Mac OS 10.7.5 版本: Rails 3.2.9,Ruby 1.9.3,MySQL 5.5.28(mysql2 gem 2.9.0),Mac OS 10.7.5
Error: 错误:
ActiveRecord::StatementInvalid in Action_figures#list
Mysql2::Error: Unknown column 'action_figures.position' in 'order clause': SELECT `action_figures`.* FROM `action_figures` ORDER BY action_figures.position ASC
Extracted source (around line #14): [list.html.erb] 提取的来源(第14行):[list.html.erb]
11: <th>Visible</th>
12: <th>Actions</th>
13: </tr>
14: <% @action_figures.each do |action_figure| %>
15: <tr>
16: <td><%= action_figure.position %></td>
17: <td><%= action_figure.name %></td>
I generated the ActionFigures model and controller, but specified :position, :name, etc. later in the migration file: 我生成了ActionFigures模型和控制器,但后面在迁移文件中指定了:position,:name等 :
class CreateActionFigures < ActiveRecord::Migration
def change
create_table :action_figures do |t|
t.string "name"
t.integer "position"
t.boolean "visible", :default => false
t.timestamps
end
end
end
And this in the model: 这在模型中:
class ActionFigure < ActiveRecord::Base
has_many :pages
attr_accessible :name, :position, :visible
end
I've run rake db:migrate
several times already, stopped and restarted the server, closed and reopened Terminal just to be sure it wasn't those things, but when I do: 我已经运行了rake db:migrate
几次,停止并重新启动服务器,关闭并重新打开终端只是为了确保它不是那些东西,但当我这样做时:
mysql> SHOW FIELDS FROM action_figures;
I get the following table: 我得到下表:
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
Questions: 问题:
If you specified the position, name, and everything else in the old ActionFigures migration file db:migrate
won't pick up the changes in that file. 如果您在旧的ActionFigures迁移文件中指定了位置,名称和其他所有内容,则db:migrate
将不会获取该文件中的更改。
If you look at the schema.rb
file in your db folder it has a version number. 如果查看db文件夹中的schema.rb
文件,它有一个版本号。 That number matches up with the number on the migration file that was last run. 该数字与上次运行的迁移文件上的数字相匹配。 So when you run rake db:migrate
it takes into consideration that number so it doesn't re-run the migrations before that. 因此,当您运行rake db:migrate
它会考虑该数字,因此在此之前不会重新运行迁移。 This happens so that tables don't try to get recreated and such... 发生这种情况,以便表不会尝试重新创建这样的...
You have a few options: 你有几个选择:
Do a rake db:rollback
to reverse the change of the creation of the ActionFigure table if you haven't done other migrations since. 如果您之后没有进行其他迁移,请执行rake db:rollback
以反转ActionFigure表创建的更改。
Do rake db:drop
then rake db:create
and rake db:migrate
to recreate the table with the changes you made in the migration. 执行rake db:drop
然后rake db:create
和rake db:migrate
以使用您在迁移中所做的更改重新创建表。
Delete the changes you made in the migration file and make a new migration using rails g migration add_name_and_position_to_action_figures name position:integer
and run rake db:migrate
to make the changes to your table. 删除您在迁移文件中所做的更改,并使用rails g migration add_name_and_position_to_action_figures name position:integer
进行新迁移并运行rake db:migrate
以对表进行更改。
There's other ways but from all I would just go with either 2 or 3 depending on the data you have on your database. 还有其他方法,但根据您在数据库中的数据,我会选择2或3。
Hope this helps! 希望这可以帮助!
Did you add the table ... run the migrations .. and then edit the migration file to add position and visible attributes? 您是否添加了表...运行迁移..然后编辑迁移文件以添加位置和可见属性? If so, you'll need to add a new migration (or rerun the last migration) 如果是这样,您将需要添加新迁移(或重新运行上次迁移)
Try this; 尝试这个;
rails g migration add_position_to_action_figures position:integer visible:boolean
bundle exec rake db:migrate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.