How to make string column with null default?

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/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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs