vidur punj
vidur punj

Reputation: 5881

How to check if a colum is present in table in rails migration

currently rails version 5.

In it I want to write the migration in condition that migration should run only when the field does not exist in the database for a particular table.

Please suggest how to implement it in rails, Thanks in advance.

Upvotes: 1

Views: 162

Answers (1)

vidur punj
vidur punj

Reputation: 5881

  def self.up
    unless ActiveRecord::Base.connection.column_exists?(:table_name, :column_name)
      add_column :table_name, :column_name, :data_type
    end
  end

Upvotes: 1

Related Questions