Rails3 Mysql2::Error: Unknown column - ActiveRecord::StatementInvalid

倖福魔咒の 提交于 2019-12-23 18:09:44

问题


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.

Versions: 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]

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:

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:

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:

  1. Why are :name, :position, and :visible not showing up in the table?
  2. How to I add them now?

回答1:


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



回答2:


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.

If you look at the schema.rb file in your db folder it has a version number. 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. This happens so that tables don't try to get recreated and such...

You have a few options:

  1. Do a rake db:rollback to reverse the change of the creation of the ActionFigure table if you haven't done other migrations since.

  2. Do rake db:drop then rake db:create and rake db:migrate to recreate the table with the changes you made in the migration.

  3. 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.

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.

Hope this helps!



来源:https://stackoverflow.com/questions/13828983/rails3-mysql2error-unknown-column-activerecordstatementinvalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!