在运行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即可。
本文介绍了一种常见的Rails迁移错误——当尝试迁移已存在的表时出现的rakeabort问题,并提供了一个简单而有效的解决方案:通过在create_table方法中添加:force=>true参数来强制更新表。
856

被折叠的 条评论
为什么被折叠?



