« Return to Thread: Mysql and non null text columns

Re: Mysql and non null text columns

by Jeremy Kemper :: Rate this Message:

Reply to Author | View in Thread


On Tue, Sep 9, 2008 at 9:47 AM, Frederick Cheung
<frederick.cheung@...> wrote:

> We've been having some problems with mysql 5 and not null text columns
> (versions 5.0.67, 5.0.51a and 5.0.51b)
>
> Mysql reports the columns as having a default of null (the column is
> actually a not null) which causes activerecord to try and insert nulls
> where it can't which makes things implode. If you do insert into foos
> values() then you do get an empty string inserted in the relevant
> column, so in that sense the column default is the empty string.
>
> Mysql is a bit funny about text columns and defaults (it won't let you
> set one, but still seems to behave as if there is one), and there are
> other places where it's funny with defaults (eg
> missing_default_forged_as_empty_string)
>
> If the column isn't marked as not null then the default is actually
> null. The behaviour with blobs is the same
>
> It seems to me that MysqlColumn#extract_default could be patched to
>
> def extract_default(default)
>   if type == :binary || type == :text
>     if default.blank?
>       null ? nil : ''
>     else
>       raise ArgumentError, "#{type} columns cannot have a default
> value: #{default.inspect}"
>     end
>   elsif missing_default_forged_as_empty_string?(default)
>     nil
>   else
>     super
>   end
> end
>
> Does this sound reasonable? Have others run into this ?

Yes, sounds good. We do our best to 'just work' with MySQL and this
quirk shouldn't be an exception.

Ideally we'd omit unassigned not-null attributes from the INSERT in
the first place.

jeremy

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-core@...
To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

 « Return to Thread: Mysql and non null text columns

LightInTheBox - Buy quality products at wholesale price!