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?
null: false
on it. This is true whether you are creating a new table or updating an existing one.NULL
in each row. In such cases, you must override this behavior with default: ""
, or else it will conflict with null: false
and the migraiton will fail.Devise uses two separate templates for building migrations: migration.rb
, for creating new tables, and migration_existing.rb
, for updating existing tables (see source on GitHub). Both templates call the same migration_data
method to generate the lines in question (i.e., the ones that specify null: false, default: ""
), but as mentioned above, default: ""
is only really relevant in the latter case (see O. Jones’ answer for more).
So the short answer to your question, specifically in the case of Devise migrations, is “because the generator uses borrowed code which doesn’t always apply, but still doesn’t break anything.”
UNIQUE
columns:Note that in most popular SQL engines, uniquely indexed columns can still contain multiple rows of NULL
values, as long as they are not required (naturally). But the effect of making a new column both required and unique (i.e., null: false
, default: ""
, and unique: true
) is that it cannot be added: the DB engine tries to populate the new column with an empty string in each row, which conflicts with the unique
constraint and causes the migration to fail.
(The only scenario in which this mechanism fails is if you have exactly one row in your table — it gets a blank string value for the new column, which naturally passes the uniqueness constraint because it's the only record.)
So another way to look at it might be that these options are a safety mechanism preventing you from running migrations that you shouldn't (i.e., retroactively adding required columns to an already-populated table).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With