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

349
Visualizações
Rails: How to modify data using migrations due to change in the schema

I have following two migrations:

One, Add column contextual_page_number to transcripts table:

class AddContextualPageNumberToTranscripts < ActiveRecord::Migration[5.2]
  def change
    add_column :transcripts, :contextual_page_number, :integer, default: 1
  end
end

Second, changing the value of the previous added column contextual_page_number based on value of another column:

class ChangePageOffsetAndContextualPageNumberOfTranscripts < ActiveRecord::Migration[5.2]
  def up
    Firm.all.find_in_batches do |group|
      group.each do |firm|
        Apartment::Tenant.switch(firm.tenant) do
          Transcript.where.not(page_offset: 0).each do |transcript|
            transcript.update(
              contextual_page_number: ((transcript.page_offset - 1) * -1),
              page_offset: 1
            )
          end
        end
      end
    end
  end

  def down
    ..
  end
end

After running the migration, I am getting unknown attribute contextual_page_number error.

== 20211108132509 AddContextualPageNumberToTranscripts: migrating ============= -- add_column(:transcripts, :contextual_page_number, :integer, {:default=>1}) -> 0.0095s == 20211108132509 AddContextualPageNumberToTranscripts: migrated (0.0096s) ====

== 20220113095658 ChangePageOffsetAndContextualPageNumberOfTranscripts: migrating rails aborted! StandardError: An error has occurred, this and all later migrations canceled:

unknown attribute 'contextual_page_number' for Transcript.

I have even tried reset_column_information, but no luck:

Apartment::Tenant.switch(firm.tenant) do
  Transcript.connection.schema_cache.clear!
  Transcript.reset_column_information
  ..
end

Any clue would be of great help, thanks.

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

0

As mentioned in one of the answer, I tried reset_column_information just right after the add_column, but that didn't worked. Finally, SQL to the rescue..

sql_cmd = "UPDATE transcripts
           SET contextual_page_number = ((page_offset - 1) * -1),
               page_offset = 1
           WHERE page_offset != 0"

Transcript.connection.execute(sql_cmd)
over 4 years ago · Santiago Trujillo Relatório

0

You need two migration files. First, try running the migration and check schema.rb for the table transcripts and verify that the newly added column contextual_page_number is being added or not.

Once you are sure that your new column is added, then again create a new migration like, eg: MigrateTransriptsCloningsData, and then add the desired changes in the up block, then execute db:migrate to update the required changes.

My choice would be To add a new rake task and executing it. like bundle exec rake migrate_transcripts_data:start instead of keeping that logic in the db/migrate/your_new_migration_file, choice is yours.

over 4 years ago · Santiago Trujillo Relatório

0

reset_column_information should be the correct way to resolve this sort of problem if you want to use models in a migration. This isn't without its problems though.

I suspect the issue is that you are calling it too late somehow. Put it first thing in the up method of the second migration or after the add_column in the first migration.

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