High Performance PostgreSQL for Rails: Storing Transformations in Generated Columns (page 85)

@andatki

The correct migration code for ruby/migration_snippet_virtual_column.rb is

class AlterTempCustomers < ActiveRecord::Migration[7.1]
  def change
    add_column 'temp.customers',
               :email_downcased, :string,
               as: 'LOWER(email)',
               stored: true
  end
end

This is because the table has already been created in the previous section. And, the book (page 85) says "Create a column for the temp schema customers table you’ve just made that adds a second column called email_downcased." The migration should be to add a column not to re-create an already existing table.

Besides, running the code from book (p.85) as-is returns error

PG::DuplicateTable: ERROR:  relation "customers" already exists

1 Like