On Feb 14, 2:17 pm, Will Bryant <[email protected]> wrote:
> On Sun, Feb 15, 2009 at 6:59 AM, ara.t.howard <[email protected]> wrote:
> > well that is indeed true - but sometimes you absolutely need this
> > functionaliy.  currently, with ar, you cannot even save a record to a
> > postgresql field which is both non-null and has no default value.  ar
> > will try to insert 'NULL' into the column and blows up.
>
> Could you explain this?  It works just fine for me.  You create a new
> model instance with that value set and it saves ok.  Of course, you
> have to explicitly give the model that value for it to work in a
> non-null, defaultless field, but that's the whole point of making it
> non-null and defaultless in the schema.


sorry - typing too fast.  i had *meant* to say a not null field with a
default value is a no go for AR:


cfp:~/rails_root > cat db/migrate/20090214040409_create_models.rb
class CreateModels < ActiveRecord::Migration
  @tablename = 'models'

  def self.up
    execute <<-sql
      create table #{ @tablename } (
         timestamp not null default current_timestamp
      )
    sql
  end

  def self.down
    execute <<-sql
      drop table #{ @tablename };
    sql
  end
end




cfp:~/rails_root > cat app/models/model.rb
class Model < ActiveRecord::Base
end



cfp:~/rails_root > ./script/runner 'p Model.create!'
/Users/ahoward/rails_root/vendor/rails/railties/lib/commands/runner.rb:
47: /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract_adapter.rb:188:in `log':
PGError: ERROR:  null value in column "foo" violates not-null
constraint (ActiveRecord::StatementInvalid)
: INSERT INTO "models" ("updated_at", "foo", "bar", "created_at")
VALUES('2009-02-15 02:43:09.452285', NULL, NULL, '2009-02-15
02:43:09.452285') RETURNING "id" from /Users/ahoward/rails_root/vendor/
rails/activerecord/lib/active_record/connection_adapters/
postgresql_adapter.rb:503:in `execute'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/postgresql_adapter.rb:1000:in
`select_raw'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/postgresql_adapter.rb:987:in
`select'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract/database_statements.rb:7:in
`select_all_without_query_cache'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract/query_cache.rb:62:in
`select_all'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract/database_statements.rb:
13:in `select_one'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/abstract/database_statements.rb:
19:in `select_value'
        from /Users/ahoward/rails_root/vendor/rails/activerecord/lib/
active_record/connection_adapters/postgresql_adapter.rb:443:in
`insert'
         ... 18 levels...
        from /Users/ahoward/rails_root/vendor/rails/railties/lib/
commands/runner.rb:47
        from /opt/local/lib/ruby/site_ruby/1.8/rubygems/
custom_require.rb:31:in `gem_original_require'
        from /opt/local/lib/ruby/site_ruby/1.8/rubygems/
custom_require.rb:31:in `require'
        from ./script/runner:3



a simple fix, with 'ar-defaults.rb'


cfp:~/rails_root > cat app/models/model.rb
class Model < ActiveRecord::Base
  def after_initialize
    self['foo'] = SQL('current_timestamp')
  end
end



cfp:~/rails_root > ./script/runner 'p Model.create!'
#<Model id: 16, foo: "current_timestamp", bar: nil, created_at:
"2009-02-15 02:44:14", updated_at: "2009-02-15 02:44:14">


bingo.

the fact is that it would be *extremely* difficult for AR to do the
'right thing' with dynamic default values like this.  or we can have a
ten line addition to handle all such cases with a simple work-around.


make sense?


cheers.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to