Rails: Default gen random uuid disappeared from column

From FVue
Jump to: navigation, search

Problem

Database table contained column, added via a migration like this:

enable_extension 'pgcrypto'
add_column :mytable1, :uuid, :uuid, default: -> { 'gen_random_uuid()' }, null: false

Environment

  • rails-7.0
  • postgresql-14


Solution

Caused by another migration with the command:

enable_extension 'pgcrypto'
add_column :mytable2, :uuid, :uuid, default: -> { 'gen_random_uuid()' }, null: false

Now as soon as you rollback this second migration, it appears that upon disabling the extension 'pgcrypto', the gen_raondom_uuid-default on mytable1 disappears as well.

So the rule is: Use this command only once in your database-migrations file-array:

enable_extension 'pgcrypto'