Kalanamith
Kalanamith

Reputation: 20648

Rake DB:Migration error when modifying the column type

I have written my migration file to change the column type as follows

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

but this gives me an error saying PG::SyntaxError: ERROR: syntax error at or near "int" LINE 2: ALTER COLUMN updated_by int I could not find the error . Thank you in advance

Upvotes: 0

Views: 905

Answers (3)

user740584
user740584

Reputation:

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

Change type of varchar field to integer: "cannot be cast automatically to type integer" could help.

Upvotes: 1

Ajay
Ajay

Reputation: 4251

As you are using SQL so accordingly you need to use ActiveRecord in this case :

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

Hope rest you can figure out easily.

Upvotes: 0

Vamsi Krishna
Vamsi Krishna

Reputation: 3782

I guess the word TYPE is needed here...

Reference: 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

Upvotes: 0

Related Questions