Rails MySQL迁移,将文本列更改为十进制

时间:2014-10-08 18:33:30

标签: mysql sql ruby-on-rails database ruby-on-rails-4

我有一个带有文本列的项目数据库,用户可以在其中输入一个数字并将其保存为字符串。 我需要将列更改为十进制,我在postgresql中使用它来完成另一个答案,但不知道如何在MySQL中执行相同的操作。

这是我在MySQL数据库上运行迁移时遇到的错误;

  

Mysql2 ::错误:十进制值不正确:''对于第-1行的列'':   ALTER TABLE items CHANGE quantity quantity decimal DEFAULT NULL

这是我运行的迁移,postgres迁移只会在我将空字符串转换为零后运行,它不会在MySQL上运行或不运行

class ChangeQuantityColumnOnItems < ActiveRecord::Migration
  def up
   if connection.adapter_name.downcase == 'postgresql'
    Item.where(quantity: '').update_all(quantity: 0)
     connection.execute(%q{
       alter table items
       alter column quantity
       type decimal using quantity::decimal
     })
   else
      Item.where(quantity: '').update_all(quantity: 0)
     change_column :items, :quantity, :decimal
   end
  end

  def down
   change_column :items, :quantity, :text
  end
end

0 个答案:

没有答案