0

I am creating a new column in a table which is a uuid/guid.

add_column :users, :uuid, :string, null: false

I am setting the uuid using:

SecureRandom.uuid

The migration fails because the existing data doesn't have any value.

How can I set the value for each user with a new uuid during this migration?

2
  • You have neglected to mention what database software you are using....MySQL? MS SQL? PostGre? Commented Oct 1, 2018 at 0:23
  • @mrunion I'm using postgres. Commented Oct 1, 2018 at 0:26

1 Answer 1

2

You have 2 options when adding a not null column:

  1. Provide a DB default
  2. Add a nullable column, update all the data, change the column to non nullable.

Depending on what DB you're using it might or might not allow you to set an expression as the default value.

Since you've mentioned you're using Postgres, you can read more here Rails syntax for providing the default will be

default: -> { 'uuid_generate_v1()' }
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.