Skip to content

Commit

Permalink
Don't set "NULL" as a constraint on nullable columns [#398 state:reso…
Browse files Browse the repository at this point in the history
…lved]

This is already the default and adding it breaks SQL standards compatibility.

Conflicts:

	activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
  • Loading branch information
tarmo committed Aug 24, 2008
1 parent 482e8fe commit ddb8c9c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
Expand Up @@ -383,13 +383,9 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:

def add_column_options!(sql, options) #:nodoc:
sql << " DEFAULT #{quote(options[:default], options[:column])}" if options_include_default?(options)
# must explcitly check for :null to allow change_column to work on migrations
if options.has_key? :null
if options[:null] == false
sql << " NOT NULL"
else
sql << " NULL"
end
# must explicitly check for :null to allow change_column to work on migrations
if options[:null] == false
sql << " NOT NULL"
end
end

Expand Down
8 changes: 4 additions & 4 deletions activerecord/test/cases/column_definition_test.rb
Expand Up @@ -9,21 +9,21 @@ def @adapter.native_database_types
end

# Avoid column definitions in create table statements like:
# `title` varchar(255) DEFAULT NULL NULL
# `title` varchar(255) DEFAULT NULL
def test_should_not_include_default_clause_when_default_is_null
column = ActiveRecord::ConnectionAdapters::Column.new("title", nil, "varchar(20)")
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
@adapter, column.name, "string",
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal "title varchar(20) NULL", column_def.to_sql
assert_equal "title varchar(20)", column_def.to_sql
end

def test_should_include_default_clause_when_default_is_present
column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)")
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
@adapter, column.name, "string",
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NULL}, column_def.to_sql
assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, column_def.to_sql
end

def test_should_specify_not_null_if_null_option_is_false
Expand All @@ -33,4 +33,4 @@ def test_should_specify_not_null_if_null_option_is_false
column.limit, column.precision, column.scale, column.default, column.null)
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
end
end
end

0 comments on commit ddb8c9c

Please sign in to comment.