Rake db:migrate error “don't know how to build

2019-02-24 20:16发布

问题:

I've got a table where I used integer on a field which needs decimal places, so I'm trying to create a migration which changes the field type from integer to float/real. My database is sqllite3 and I'm using rails3.

I ran

rails generate migration ChangeMeasureColumnOnIngredients

to create the initial migration files, then updated the class to

class ChangeMeasureColumnOnIngredients < ActiveRecord::Migration
  def self.up
    change_column :ingredients, :measure, :real
  end

I ran rake db:migrate and it returned fine.

When I inserted a value via my rails app, it didn't return the decimal place. I started to think that many rails doesn't know what 'real' is as a datatype, so I changed the migration to

change_column :ingredients, :measure, :float

I then ran

rake db:migrate change_measure_column_on_ingredients
and now I get the following error
c:\Ruby192\rails3rc>rake db:migrate change_measure_column_on_ingredients
(in c:/Ruby192/rails3rc)
rake aborted!
Don't know how to build task 'change_measure_column_on_ingredients'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:1720:in []'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2040:ininvoke_task'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:in block (2 levels) in top_level'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:ineach'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:in block in top_level'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2058:instandard_exception_handling'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2013:in top_level'
C:/Ruby192/lib/ruby/1.9.1/rake.rb:1992:inrun'
C:/Ruby192/bin/rake:31:in `'

I tried changing the :float back to :real, but I still get that error.

can somebody tell me what I'm doing wrong? I'm new to rails and still learning.

回答1:

Your rake call has instructed rake to build the task db:migrate followed by the task change_measure_column_on_ingredients which clearly isn't want you want as the latter is not a rake task.

To run a specific migration you need to provide the VERSION of the migration. This is the number in the file name which comes before your name for the migration. You can migrate it up or down like this:

rake db:migrate:down VERSION=123456789
rake db:migrate:up VERSION=123456789

Alternatively you can take the last migration down then up by doing the following (you can also specify a VERSION for this):

rake db:migrate:redo

There are other options though. If you run rake --describe db:migrate you'll get some more information.



回答2:

While in this specific instance the stacktrace posted by OP shows the error is trying to do two tasks at once, I found this page after googling and just want to add to the answer for future googlers:

try including either RAILS_ENV=development or RAILS_ENV=test as that is what fixed it for me.



回答3:

make sure your command is rake db:migrate . Pay attention to there is no any space between : and migrate