Rake DB:修改列类型时出现迁移错误

时间:2013-09-10 10:59:53

标签: ruby-on-rails postgresql

我编写了我的迁移文件以更改列类型,如下所示

class ChangeColumnTypeInMyTable < ActiveRecord::Migration
   def self.up




     execute <<-SQL
       ALTER TABLE batches
         ALTER COLUMN updated_by int
      SQL

     execute <<-SQL
        ALTER TABLE batches
          ALTER COLUMN created_by int
     SQL

 end

def self.down
  end
end

但是这给我一个错误说PG::SyntaxError: ERROR: syntax error at or near "int" LINE 2: ALTER COLUMN updated_by int 我找不到错误。提前谢谢

3 个答案:

答案 0 :(得分:1)

ALTER COLUMN updated_by TYPE int USING (updated_by::integer)

Change type of varchar field to integer: "cannot be cast automatically to type integer"可以提供帮助。

答案 1 :(得分:0)

我想这里需要TYPE这个词......

参考:http://www.postgresql.org/docs/8.0/static/sql-altertable.html

class ChangeColumnTypeInMyTable < ActiveRecord::Migration

  def self.up
    execute <<-SQL
     ALTER TABLE mt940_batches
     ALTER COLUMN updated_by TYPE int
    SQL

    execute <<-SQL
     ALTER TABLE mt940_batches
     ALTER COLUMN created_by TYPE int
    SQL
  end

  def self.down
  end
end

答案 2 :(得分:0)

因为您正在使用SQL,所以在这种情况下您需要使用ActiveRecord:

def self.up
connection = ActiveRecord::Base.connection()
connection.execute(put_your_sql_query_here)
end

希望你可以轻松搞清楚。

相关问题