Ruby on Rails Wednesday, March 30, 2011

On Wed, Mar 30, 2011 at 12:11 PM, James Byrne <lists@ruby-forum.com> wrote:

I have a model in whose migration I have defined a datetime column that
both has a default value and sets null to false.  As this is a
PostgreSQL database the default value is 'infinity', which may be the
source of the difficulty.

When I attempt to save this model ActiveRecord produces an SQL statement
that explicitly sets this column to NULL thus both defeating the purpose
of the default value and triggering the NOT NULL constraint on the DBMS.

How does one turn off or override this behaviour?  What I want to happen
is that model_instance.save will only specify columns that have values
set and will simply not include any column that does not have an
explicit value set.

I can see that for other attributes ActiveRecord is setting things to
the defaults provided in the migration. Is this the case? If not then
where is ActiveRecord getting this information?  And why bother?  The
purpose of a default value on a database column is to have the DBMS
provide for the absence of information at the DBMS level.  If I wish to
do that at the attribute level then I can do so in the model class
itself.


It sounds like you default of "infinity" is not being honored at all. Most likely because it is not a valid datetime and ActiveRecord doesn't know what to set it as. In irb if you create a new instance of that model (model.new) you should see all the default values set. If you are not seeing one for datetime then "infinity" is not being honored as a valid datetime. I would suggest changing "infinity" to a datetime that is 50 years into the future.

Good luck.

B. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment