在运行rake db:migrate时,有时会发现 rake abort 的错误,rake说某个表已经存在,因此这个migrate的工作它干不了。这个表确实是已经存在的,那么如何让rake跳过这个表,或是强制覆写这个表呢?有一处需要修改:
原migrate文件:
修改后的migrate文件:
看出来了吗,在create_table的参数中,加上 :force => true即可。
原migrate文件:
ruby 代码
- class CreateProducts < ActiveRecord::Migration
- def self.up
- create_table :products do |t|
- t.column :title, :string
- t.column :description, :text
- t.column :image_url, :string
- end
- end
- def self.down
- drop_table :products
- end
- end
修改后的migrate文件:
ruby 代码
- class CreateProducts < ActiveRecord::Migration
- def self.up
- create_table :products, :force => true do |t|
- t.column :title, :string
- t.column :description, :text
- t.column :image_url, :string
- end
- end
- def self.down
- drop_table :products
- end
- end
看出来了吗,在create_table的参数中,加上 :force => true即可。