datamapper / do

DataObjects
147 stars 73 forks source link

default date/time doesn't work properly in postgres #14

Closed tkellen closed 13 years ago

tkellen commented 13 years ago
DataMapper.setup(:default, 'postgres://localhost/bug')

class Bug
  include DataMapper::Resource

  property :id, Serial
  property :date_added, Date, :required => true,
                              :default => DateTime.now
end

DataMapper.finalize

creates this:

bug=# \d bugs;
                           Table "public.bugs"
   Column   |  Type   |                     Modifiers                     
------------+---------+---------------------------------------------------
 id         | integer | not null default nextval('bugs_id_seq'::regclass)
 date_added | date    | not null default '2011-09-11'::date
Indexes:
    "bugs_pkey" PRIMARY KEY, btree (id)

...the actual date/time is set correctly when creating records but this schema will not play nicely with apps that interact with the database through some other method.

dkubb commented 13 years ago

@tkellen: a default like this will be evaluated when the class is, so your're essentially hard-coding the date/time in your model. What you want to use is a proc: :default => proc { DateTime.now }. This will be evaluated each time an object is created.

See the section titled "Setting default values" in http://datamapper.org/docs/properties.html for more information.