Remove default value from column on migration

I need to remove a default value on a PostgreSQL database column in a
Rails migration.

The table contains a column which is a string with :null =>
true, :default => nil. I need to migrate to :null => false, but the
end result in the database’s DDL has a default value of “NULL” (a
string). How could I get rid of this?

I have looked at change_column_default which didn’t get me too far.
I’m looking for something along the lines of
“remove_column_default :the_table, :the_column”. Is this possible?

Thanks,

-Harold

Harold wrote:

I need to remove a default value on a PostgreSQL database column in a
Rails migration.

The table contains a column which is a string with :null =>
true, :default => nil. I need to migrate to :null => false, but the
end result in the database’s DDL has a default value of “NULL” (a
string). How could I get rid of this?

I have looked at change_column_default which didn’t get me too far.
I’m looking for something along the lines of
“remove_column_default :the_table, :the_column”. Is this possible?

Thanks,

-Harold

change_column(table_name, column_name, type, options): Changes the
column to a different type using the same parameters as add_column.

Original Migration:
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books, :force => true do |t|
t.string :name, :default=>“NUlL”
t.timestamps
t.integer :lock_version, :null => false, :default => 0
end
end

def self.down
end
end

#script/console

Book.new
=> #<Book id: nil, name: “NUlL”, created_at: nil, updated_at: nil,
lock_version: 0>

Modify The Book’s Name Column:
class ModifyBookNameColumn < ActiveRecord::Migration
def self.up
change_column :books, :name, :string, :default => “Lake”
end

def self.down
end
end

#script/console (notice the name is not “NULL” but is “Lake”)

Book.new
=> #<Book id: nil, name: “Lake”, created_at: nil, updated_at: nil,
lock_version: 0>

Lake, thanks for your answer. However, my migration does exactly this
(change_column). The problem is that I do not want a default value on
the resulting column.

To continue with the example you provided, if I do:

change_column :books, :name, :string, :default => “Lake”

I am making the default for the column be “Lake”, which is not the
intended result. Ideally, I would simply say:

change_column :books, :name, :string, :null => false

In which case the name column in the books table is not nullable (this
is what I want). My original column was nullable, and had a default
value of NULL, but when this migration is ran, the resulting column
has a default value of “NULL” as a string, which is what I want to
avoid…

If I try

change_column :books, :name, :string, :null => false, :default => nil

I end up with the same result (default value of “NULL”).

How can I get rid of the default value for this table all together?

One possible solution is to do:

add_column :books, :temp_name, :string, :null =3D> false
#store names on new temp_name column
Book.find(:all).each do |b|
b.name.nil? b.temp_name => b.name : b.temp_name = ‘Unknown’
b.save!
end
remove_column :books, :name
rename_column :books, :temp_name, :name

I’m wondering if there is an easier way though…

Thanks again,

-H

PS: What happened to this group for the better part of the day? It was
inaccessible for a while…

On Aug 11, 5:40 pm, Lake D. [email protected]