论坛首页 编程语言技术论坛

db:migrate时,Table already exist 的解决办法

浏览 3508 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-23  
        在运行rake db:migrate时,有时会发现 rake abort 的错误,rake说某个表已经存在,因此这个migrate的工作它干不了。这个表确实是已经存在的,那么如何让rake跳过这个表,或是强制覆写这个表呢?有一处需要修改:

原migrate文件:
ruby 代码
 
  1. class CreateProducts < ActiveRecord::Migration  
  2.   def self.up  
  3.     create_table :products do |t|  
  4.       t.column :title:string  
  5.       t.column :description:text  
  6.       t.column :image_url:string  
  7.     end  
  8.   end  
  9.   
  10.   def self.down  
  11.     drop_table :products  
  12.   end  
  13. end  



修改后的migrate文件:
ruby 代码
 
  1. class CreateProducts < ActiveRecord::Migration  
  2.   def self.up  
  3.     create_table :products:force => true do |t|  
  4.       t.column :title:string  
  5.       t.column :description:text  
  6.       t.column :image_url:string  
  7.     end  
  8.   end  
  9.   
  10.   def self.down  
  11.     drop_table :products  
  12.   end  
  13. end  

看出来了吗,在create_table的参数中,加上 :force => true即可。
   发表时间:2007-11-24  
学习了,我以前也经常出现这种问题,只知道不行了用version重新来。看来要好好学习Ruby基础了。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics