# How to make string column with null default?

**URL:** <https://www.ruby-forum.com/t/how-to-make-string-column-with-null-default/114043>\
**Category:** Rails\
**Created:** [September 7, 2007, 9:39am UTC](https://www.ruby-forum.com/t/how-to-make-string-column-with-null-default/114043 "2007-09-07T09:39:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Steven](https://www.ruby-forum.com/u/Steven)\
**Post date:** [September 7, 2007, 9:39am UTC](https://www.ruby-forum.com/t/how-to-make-string-column-with-null-default/114043/1 "2007-09-07T09:39:09Z")

</div>

Hi,

I’m trying to use migrations to create a table with a string column  
that has a default value of null. Instead of a default of null,  
though, it’s using the four-character string “NULL”.

I found some similar bug reports (e.g.,  
[http://dev.rubyonrails.org/ticket/9469](http://dev.rubyonrails.org/ticket/9469)  
[http://dev.rubyonrails.org/ticket/9145](http://dev.rubyonrails.org/ticket/9145) ), but that second one was  
closed with “cannot reproduce/works for me”, so I must be doing  
something wrong???

This is what I’m doing. I tried this with gem rails 1.2.3 and edge  
rails 7416, and they both did the same thing.

$ rails project ; cd project  
$ cat \> config/database.yml  
development:  
adapter: sqlite3  
database: db/project.development  
^D  
$ mkdir db/migrate ; cat \> db/migrate/001\_initial\_schema.rb  
class InitialSchema \< ActiveRecord::Migration  
def self.up  
create\_table :items do |t|  
t.column :name, :string  
# I also tried:  
# t.column :name, :string, :null =\> true, :default =\> nil  
end  
end  
end  
^D  
$ cat \> app/models/item.rb  
class Item \< ActiveRecord::Base  
end  
^D  
$ rake db:migrate

$ script/console  
Loading development environment.

> > x = Item.create  
> > =\> #\<Item id: 1, name: “NULL”\>  
> > x.name.length  
> > =\> 4  
> > ^D

$ sqlite3 db/project.development  
sqlite\> .schema items  
CREATE TABLE items (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
“name” varchar(255) DEFAULT NULL);  
sqlite\> insert into items values (69, null);  
sqlite\> select \* from items;  
1|NULL  
69|

Thanks for you help,  
Steven
