Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Change column data type from boolean to integer in Rails

I have a column called :active in one of my database tables (I'm using postgres) that is currently a boolean value, and is defaulted to true. I'd like to change this column's type from boolean to integer, so that I can use an enum going forward with this project. I've checked the documentation and some stack overflow answers, but haven't found a solid answer yet. How do I do make this change using migrations? I am using Rails 5, if that makes a difference. Thanks!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Your migration file should look like this

def change
    change_column :table_name, :active, :integer
end

Also you need to rewrite all the previous records to an integer value which can be done in the same migration file.

over 4 years ago · Santiago Trujillo Relatório

0

You can create a new migration with:

rails g migration change_data_type_for_active

Then edit the migration to use change_column:

class ChangeDataTypeForActive < ActiveRecord::Migration

  def self.up
    change_column :table_name, :active, :integer
  end

  def self.down
    change_column :table_name, :active, :boolean
  end

end

Then run the migration:

rake db:migrate

The reason to define up and down methods is that you can return you database to the previos state with:

rake db:rollback
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda