# Losing Default Value with Migration

**URL:** <https://www.ruby-forum.com/t/losing-default-value-with-migration/107534>\
**Category:** Rails\
**Created:** [June 22, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/losing-default-value-with-migration/107534 "2007-06-22T18:03:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennis\_baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dennis\_baldwin](https://www.ruby-forum.com/u/dennis_baldwin)\
**Post date:** [June 22, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/losing-default-value-with-migration/107534/1 "2007-06-22T18:03:54Z")

</div>

Hi,

I’m wondering if anyone has experienced problems with losing default  
column values when running a migration. I have a column that defaults  
to a value of 0, yet when I run a migration I lose the default value  
which reverts to NULL. My migration is simple:

class RenameRecentLocationId \< ActiveRecord::Migration  
def self.up  
rename\_column :devices, :recent\_location\_id, :recent\_reading\_id  
end

def self.down  
rename\_column :devices, :recent\_reading\_id, :recent\_location\_id  
end  
end

Am I missing something or is this a known issue? Thanks in advance  
for any light you can shed on the problem.

Best regards,  
Dennis

---

<div class="post-metadata">

**Author:** ![dennis\_baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dennis\_baldwin](https://www.ruby-forum.com/u/dennis_baldwin)\
**Post date:** [June 22, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/losing-default-value-with-migration/107534/2 "2007-06-22T18:19:59Z")

</div>

Okay, so it seems I should do a change\_column after the rename. Sorry  
for the waste of bytes and I was expecting Rails to do everything for  
me with one line of code 🙂

class RenameRecentLocationId \< ActiveRecord::Migration  
def self.up  
rename\_column :devices, :recent\_location\_id, :recent\_reading\_id  
change\_column :devices, :recent\_reading\_id, :integer, :default =\>  
0  
end

def self.down  
rename\_column :devices, :recent\_reading\_id, :recent\_location\_id  
change\_column :devices, :recent\_location\_id, :integer, :default =\>  
0  
end  
end
