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

**URL:** <https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265>\
**Category:** PragProg Customers\
**Tags:** book-high-performance-postgresql-for-rails, errata\
**Created:** [22 September 2024 14:59 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265 "2024-09-22T14:59:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanctusgee](https://forum.devtalk.com/user_avatar/forum.devtalk.com/sanctusgee/32/6595_2.png) [@sanctusgee](https://forum.devtalk.com/u/sanctusgee)\
**Post date:** [22 September 2024 14:59 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265/1 "2024-09-22T14:59:25Z")

</div>

### @andatki

The correct migration code for `ruby/migration_snippet_virtual_column.rb` is

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![andatki](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andatki/32/5332_2.png) [@andatki](https://forum.devtalk.com/u/andatki)\
**Post date:** [9 October 2024 20:46 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265/2 "2024-10-09T20:46:55Z")

</div>

Thank you for reporting this @santusgee. That’s indeed a mistake and was likely due to writing examples out of order. We’ll get a fix incorporated for the next ebook release and I’ll leave this open until that’s ready to go, and can follow up.

---

<div class="post-metadata">

**Author:** ![andatki](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andatki/32/5332_2.png) [@andatki](https://forum.devtalk.com/u/andatki)\
**Post date:** [2 May 2025 20:16 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265/3 "2025-05-02T20:16:47Z")

</div>

Hi @sanctusgee. I’m submitting an update to the book text based on your post here. Hopefully in lands in a new version soon.

I changed the migration code snippet you mentioned away from `create_table` and to `add_column` as follows:

```rb
add_column 'temp.customers',
  :email_downcased,
  :string,
  as: 'LOWER(email)',
  stored: true

```

As you said, the `temp.customers` (temp schema, customers table) table should exist already. I re-made it in my local testing environment quick like this:

```sql
create table if not exists temp.customers (id integer, email text);

```

With that snippet, I generated a dummy “Foo” Rails migration.

I pasted the code above into the `change` method and ran it:

```rb
-- add_column("temp.customers", :email_downcased, :string, {:as=>"LOWER(email)", :stored=>true})
   (3.1ms) ALTER TABLE "temp"."customers" ADD "email_downcased" character varying GENERATED ALWAYS AS (LOWER(email)) STORED /*application='Rideshare'*/

```

Now when I `\d temp.customers`, I see the generated, stored virtual column, with the `LOWER()` function for `email`.

Look good?

Thanks again!

---

<div class="post-metadata">

**Author:** ![andatki](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andatki/32/5332_2.png) [@andatki](https://forum.devtalk.com/u/andatki)\
**Post date:** [12 July 2025 03:22 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-storing-transformations-in-generated-columns-page-85/171265/4 "2025-07-12T03:22:17Z")

</div>

Closed on 12 July 2025 03:22 UTC


