Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between default and null in migration

What is the difference between:

t.boolean :test, :default => true

and

t.boolean :test, :null => true

and

t.boolean :test, :default => true, :null => true

EDIT

Does the following make any sense?

t.boolean :test, :default => true, :null => false
like image 339
glarkou Avatar asked Jul 09 '11 16:07

glarkou


People also ask

What is the difference between default and null?

There's no difference. The default value of any reference type is null .

Is null the default value?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.

What is null false in rails migration?

The null: false parameter means this column does not allow NULL values. The default: false tells us this column will default to false whenever a value isn't specified. This is nice because we can guarantee this field will always be either true or false now. It can't be null because the database prevents it.

What does null true mean rails?

null=True will set the field's value to NULL i.e., no data. It is basically for the databases column value.


2 Answers

"null" means "are you allowed to enter a null value in this column"?

Whereas "default" means "if there is a null value in this column ... then use this default value instead"

So, for your examples:

t.boolean :test, :default => true

"this boolean column will insert a true if you don't bother setting a value for it"

t.boolean :test, :null => true

"this boolean column will let you set it to true, false or null - and it will stay the way you set it"

t.boolean :test, :default => true, :null => true

"this boolean column will let you set it to true, false or null... but if you set it to null it will automatically be set to true"

like image 171
Taryn East Avatar answered Oct 02 '22 15:10

Taryn East


:default - The column’s default value. Use nil for NULL.

:null - Allows or disallows NULL values in the column. This option could have been named :null_allowed.

In the first option, if you don't specify anything, rails will put true In the second option, it will allow the value to be null. In the third option, both apply, the values can be true, false and nil

like image 28
Thiago Avatar answered Oct 02 '22 15:10

Thiago