I'm building a Rails app with Devise for authentication, and its default DB migration sets the following columns:
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
What is the purpose of setting null: false
and default: ""
at the same time?
My understanding is that null: false
effectively makes a value required: i.e., trying to save a record with a NULL
value in that column will fail on the database level, without depending any validations on the model.
But then default: ""
basically undoes that by simply converting NULL
values to empty strings before saving.
I understand that for an optional column, you want to reject NULL
values just to make sure that all data within that column is of the same type. However, in this case, email and password are decidedly not optional attributes on a user authentication model. I'm sure there are validations in the model to make sure you can't create a user with an empty email address, but why would you set default: ""
here in the first place? Does it serve some benefit or prevent some edge case that I haven't considered?