Forum: Ruby on Rails Default values in postgres not working

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
21d31516c1307a76b4c95cc853f3d169?d=identicon&s=25 Tessa Lau (Guest)
on 2007-05-25 02:50
(Received via mailing list)
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
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

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

    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
the development environment, zoom has a valid default!

  % script/console test
  Loading test environment.
  => #<Map:0xb7479b4c @new_record=true, @attributes={"zoom"=>nil}>

  % script/console development
  Loading development environment.
  => #<Map:0xb7414f1c @new_record=true, @attributes={"zoom"=>1.0}>

I also noticed a difference in the two database schemas when compared
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
honored.  I deployed this app on a production server, and the production
environment on the server does *not* have default values.  Yet I checked
all the migrations are up to date, and in fact that column would not
unless I'm at the current migration.  It's just the default value that's

Help?  Isn't this supposed to Just Work?
This topic is locked and can not be replied to.