10

I am trying to understand the following statement, it is from a rails migration file:

x.datetime "new",     :null => false
x.datetime "update",  :null => false

I understand the the first part of both statements (everything before the comma) but I am unsure on the null portion

:null => false

Is this basically saying "if it does not exist, then it is false?" The logic just seems a bit strange, any clarification on this would be greatly helpful.

FluxEngine
  • 12,730
  • 14
  • 57
  • 83
  • It's just a hash argument. – Dave Newton Mar 12 '13 at 17:59
  • 6
    @DaveNewton: If the OP can't read the quoted statements, he's fairly unlikely to understand that comment, wouldn't you say? – T.J. Crowder Mar 12 '13 at 18:00
  • 1
    Well, `=> false` would define a [bound](http://coffeescript.org/#fat-arrow) `function () { return false; }`. But, `:null` is a syntax error. If it was `null:` instead, then it would be a [key for an `Object`](http://coffeescript.org/#objects_and_arrays). – Jonathan Lonowski Mar 12 '13 at 18:08
  • 5
    Are you sure that's CoffeeScript? Looks more like something from a Rails migration to me. That sort of thing would create a couple new timestamp columns that don't allow NULLs. – mu is too short Mar 12 '13 at 18:19
  • are you sure this is coffeescript? This looks very much as a ruby where the last paramter is a hash – robkuz Mar 12 '13 at 18:22
  • @muistooshort you are right this is from a rails migration, I am trying to work with them in coffee script. – FluxEngine Mar 12 '13 at 18:27
  • You're trying to ... what ?! Doesn't make any sense to create rails migrations in Coffeescript – Anthony Alberto Mar 12 '13 at 18:29
  • I am using the variables "new" & "update" in coffeescript (for a controller I am writing), the text from above is from a rails migration. I am not sure what the :null => false means exactly. – FluxEngine Mar 12 '13 at 18:37

3 Answers3

30

:null => false in a Rails migration tells your database not to accept NULL values. It can be used with :default => 0 to tell your database to use '0' as the default value (a) when NULL or nothing is specified in a query or (b) when creating or updating an object. (Remember, '0' and NULL are not the same things.)

Peter Bloom
  • 1,302
  • 14
  • 21
6

Firstly, rather than use x I'd use the standard t variable that is used in migrations.

Now, inside migration files the t object in create_table is actually of type ActiveRecord::ConnectionAdapters::TableDefinition.

And,

t.datetime "new",    :null => false
t.datetime "update", :null => false

actually translates to

t.column("new", :datetime, { :null => false })
t.column("update", :datetime, { :null => false })

where the last argument is the options argument of the column method.

According to the documentation one of these options is :null which allows or disallows NULL values in the column.

So, in summary :null => false would mean "Do not allow NULL values in the new or update column".

Dwayne Crooks
  • 2,737
  • 25
  • 26
3

Edit: I had taken the question to be about syntax and translation since it originally mentioned CoffeeScript. For purpose, refer to Peter Bloom's answer.


I am not sure what the :null => false means exactly.

The => operator is a key/value separator in Ruby, defining a Hash with a :null key set to false. It's similar to : for an Object literal in CoffeeScript/JavaScript -- { null: false }.

When used in an argument list, it's one option for allowing/imitating named arguments in Ruby.

The other main difference is that CoffeeScript/JavaScript use Strings for keys while Ruby typically uses Symbols -- "null" (cs/js) vs. :null (rb).

So, the syntactic equivalent in CoffeeScript would be:

x.datetime "new",     null: false
x.datetime "update",  null: false

In JavaScript, that's:

x.datetime("new",    { null: false });
x.datetime("update", { null: false });
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • The [answer](http://stackoverflow.com/a/16598317/2909897) below this answered my question in the right context. – mbigras Feb 12 '17 at 23:04