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

262
Visualizações
create rails migration for PostgreSQL GENERATED ALWAYS AS column

How to create rails migration for auto calculated columns like below.

    a_column integer,
    b_column integer,
    total1 integer GENERATED ALWAYS AS (a_column+b_column) stored,
    total2 integer GENERATED ALWAYS AS (a_column-b_column) stored,
    total3 integer GENERATED ALWAYS AS (a_column/b_column) stored
); 

I don't want to do using Models, because I want to use same table from another system having different technology.

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

0

You can use pure SQL in migration

def up
  execute <<~SQL
    CREATE TABLE table_name (
      a_column integer,
      b_column integer,
      total1 integer GENERATED ALWAYS AS (a_column+b_column) stored,
      total2 integer GENERATED ALWAYS AS (a_column-b_column) stored,
      total3 integer GENERATED ALWAYS AS (a_column/b_column) stored
    );
  SQL
end

def down
  drop_table :table_name
end

When using such migrations, in order for the database schema to be correct, you need to use SQL dump type (default type is Ruby). To setup this setting add line to your config/application.rb

config.active_record.schema_format = :sql

After that schema will be saved to db/structure.sql

Please read more

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