# Remove default value from column on migration

**URL:** <https://discuss.rubyonrails.org/t/remove-default-value-from-column-on-migration/33102>\
**Category:** rubyonrails-talk\
**Created:** [August 11, 2008, 6:59pm UTC](https://discuss.rubyonrails.org/t/remove-default-value-from-column-on-migration/33102 "2008-08-11T18:59:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Harold](https://discuss.rubyonrails.org/u/Harold)\
**Post date:** [August 11, 2008, 6:59pm UTC](https://discuss.rubyonrails.org/t/remove-default-value-from-column-on-migration/33102/1 "2008-08-11T18:59:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Harold](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Harold](https://discuss.rubyonrails.org/u/Harold)\
**Post date:** [August 12, 2008, 10:46pm UTC](https://discuss.rubyonrails.org/t/remove-default-value-from-column-on-migration/33102/2 "2008-08-12T22:46:57Z")

</div>

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| &nbsp;&nbsp;b.name.nil? b.temp\_name =\> b.name : b.temp\_name = 'Unknown' &nbsp;&nbsp;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...
