There is a problem while ActiveRecord generates default values in CREATE TABLE statement:
when table is defined
create_table :reservations do |t|
t.column :test_column, :string
end
generated sql command looks like this:
CREATE TABLE reservations ([id] int NOT NULL IDENTITY(1, 1) PRIMARY KEY, [test_column] varchar(255) DEFAULT NULL)
in MS SQL (sqlserver adapter) it causes storing default string value "NULL" instead of NULL
problem is in to_sql method in ColumnDefinition (schema_definitions.rb) where is called
add_column_options!(column_sql, :null => null, :default => default) unless type.to_sym == :primary_key
that's causes in add_column_options! method that DEFAULT is always added for columns that allows null values
I think that this code is better (from previous revision)
sql << " DEFAULT #{quote(options[:default], options[:column])}" unless options[:default].nil?
than actual
sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options)
...
def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end