Default values in postgres not working

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?

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