ppdai 2007-11-5 23:45
关于数据迁移的问题
需要在数据库中添加一个字段
我先运行ruby script/generate migration add_price
然后打开迁移任务的源文件,修改其中的up()方法,想表products中添加price字段。down()方法删除这个字段
class AddPrice < ActiveRecord::Migration
def self.up
add_column :products, :decimal, :precision =>8, :scale=>2, :default=>0
end
def self.down
remove_column :products, :price
end
end
运行数据迁移:
rake db:migrate
[img=656,161]C:\Documents and Settings\Administrator\桌面\QQ截图未命名.bmp[/img]
add_column :products, :decimal, :precision =>8, :scale=>2, :default=>0
rake aborted!
You have a nil obiect when you didn't expect it!
You might have expexted an instance of Array.
The error occurred while evaluating nil.[]
请问有啥解决方案?
[[i] 本帖最后由 ppdai 于 2007-11-5 23:55 编辑 [/i]]
ppdai 2007-11-5 23:47
[img]C:\Documents and Settings\Administrator\桌面\QQ截图未命名.bmp[/img]
[[i] 本帖最后由 ppdai 于 2007-11-5 23:51 编辑 [/i]]
lgn21st 2007-11-6 02:39
[code]
class AddPrice < ActiveRecord::Migration
def self.up
add_column :products, :decimal, :precision =>8, :scale=>2, :default=>0
end
def self.down
remove_column :products, :price
end
end
[/code]
看不到楼主的图,我就猜一下吧,根据楼主贴出来的代码,我认为出错的地方在
add_column这个方法
add_column "表名", "字段名", "数据类型", "初始化值"
所以楼主尝试修改成
add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0
试试看