# Default values in postgres not working

**URL:** <https://www.ruby-forum.com/t/default-values-in-postgres-not-working/105030>\
**Category:** Rails\
**Created:** [May 25, 2007, 2:50am UTC](https://www.ruby-forum.com/t/default-values-in-postgres-not-working/105030 "2007-05-25T02:50:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tessa\_Lau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de0a6/32.png) [@Tessa\_Lau](https://www.ruby-forum.com/u/Tessa_Lau)\
**Post date:** [May 25, 2007, 2:50am UTC](https://www.ruby-forum.com/t/default-values-in-postgres-not-working/105030/1 "2007-05-25T02:50:45Z")

</div>

I used a migration to add a column to my database that has a default  
value and a  
not-null constraint. Sometimes this default value is honored by  
postgres,  
sometimes it is not. When it’s not being honored, I get errors like the  
following from postgres:

PGError: ERROR: null value in column “zoom” violates not-null  
constraint

However, I declared this column as having a default value in my  
migration:

```
add_column(:maps, :zoom, :float, { :null => false, :default => 1.0 

```

})

I have a unit test that checks that the default zoom value is 1, but the  
test is  
failing because the zoom value is nil. However, when I create a new Map  
using  
the development environment, zoom has a valid default!

% script/console test  
Loading test environment.  
console\>\> Map.new  
=\> #\<Map:0xb7479b4c @new\_record=true, @attributes={“zoom”=\>nil}\>

% script/console development  
Loading development environment.  
console\>\> Map.new  
=\> #\<Map:0xb7414f1c @new\_record=true, @attributes={“zoom”=\>1.0}\>

I also noticed a difference in the two database schemas when compared  
using  
pg\_dump. The test schema has this column description:

```
zoom double precision DEFAULT (1)::double precision NOT NULL,

```

The development schema has this one:

```
zoom double precision DEFAULT 1::double precision NOT NULL,

```

The parenthesis around the (1) in the test schema seems to be indicative  
of the  
problem – whenever I find this in the schema, the default value isn’t  
being  
honored. I deployed this app on a production server, and the production  
environment on the server does _not_ have default values. Yet I checked  
that  
all the migrations are up to date, and in fact that column would not  
exist  
unless I’m at the current migration. It’s just the default value that’s  
missing.

Help? Isn’t this supposed to Just Work?
