3

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?

1
  • 1
    There is no limitation on PostgreSQL side for this type. I just set NOT NULL with no issues. You could work around this problem by trying to use change_column_null, tho I suppose it does not answer you question. Commented Apr 18, 2017 at 11:58

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.