Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Postgres migrate uuid column type with default value doesn't work

My migration is

class AddUuidToUsers < ActiveRecord::Migration
  def change
    add_column :users, :uuid, :uuid, null: false, default: 'uuid_generate_v4()'
    add_index :users, :uuid
  end
end

The schema is changed with:

t.uuid     "uuid",                               default: "uuid_generate_v4()"

As you see, we the null: false is not added, not sure why?

But when I try with a common column type for instance string it works well.

Is there any limitation for :uuid column type that we can't use null: false?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Rails 4.2 totally ignore null option for uuid columns with a default value. You may check a column object for your model:

> User.columns[2] # number of uuid column
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007fe6fee53f20 ...
@sql_type="uuid", @null=true, @default=nil, @default_function="uuid_generate_v4()">

But migration options was the same as in your example:

add_column :users, :uuid, :uuid, null: false, default: 'uuid_generate_v4()'

Rails 5.0 had fixed this bug. Column object keeps null option for the same migration:

> User.columns[2]
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007fa6c3722650 ...
@sql_type="uuid", @type=:uuid, @null=false, @default=nil, @default_function="uuid_generate_v4()", @collation=nil, @comment=nil>

And schema.rb also contains null: false:

t.uuid   "uuid", default: -> { "uuid_generate_v4()" }, null: false
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!