Forum: Ruby on Rails ActiveRecord failing with a default field value of blank

Posted by Duane Morin (dmorin)
on 2009-10-13 22:32
I have a bug in my code that's caught me completely off guard, and I'm
hoping that maybe my IT folks updated a gem somewhere that changed
something I didn't know about.

I've got a field, mq_unsubscribe, in my mail_queue table.  It has a
non-null constraint on it, and a default value of empty string ''.  (I
did not set it up.)

I've got a simple Model pointed at this table, and have been generating
items like this:

MailQueueItem.create :template=>3, :subject=>'Foo', :email=>'duane'..

and so on, which turns nicely into an INSERT into mail_queue
(`mq_unsubscribe`, `template`, `subject`, `email`) VALUES (``, 3, `Foo`,
`duane`)

Note that it recognizes the empty string, and uses that.

However, I just discovered I'm now getting this:

INSERT into mail_queue (`mq_unsubscribe`, `template`, `subject`,
`email`) VALUES (NULL, 3, `Foo`, `duane`)

where it's generating a NULL for my non-specified field, and that's
breaking the constraint.  To the best of my ability to figure it out,
the code that does the MailQueueItem.create has not in any way changed.

I'm hoping that somebody can say "Yes, version X of ActiveRecord now
behaves differently in that circumstance..."

Fingers crossed!

Duane
Posted by Frederick Cheung (Guest)
on 2009-10-13 23:34
(Received via mailing list)
On Oct 13, 9:32 pm, Duane Morin <rails-mailing-l...@andreas-s.net>
wrote:
>
> where it's generating a NULL for my non-specified field, and that's
> breaking the constraint.  To the best of my ability to figure it out,
> the code that does the MailQueueItem.create has not in any way changed.
>
> I'm hoping that somebody can say "Yes, version X of ActiveRecord now
> behaves differently in that circumstance..."
>

I do remember some changes on how defaults on text columns on mysql
are handled. Does that sounds relevant ? Did your version of
activerecord change ? (if so from what to what ?)

Fred
Posted by Greg Donald (destiney)
on 2009-10-14 01:41
(Received via mailing list)
On Tue, Oct 13, 2009 at 3:32 PM, Duane Morin
<rails-mailing-list@andreas-s.net> wrote:
> I've got a field, mq_unsubscribe, in my mail_queue table.  It has a
> non-null constraint on it, and a default value of empty string ''.  (I
> did not set it up.)

I'd fix the problem here by making the default null.

--
Greg Donald
http://destiney.com/
Posted by Duane Morin (dmorin)
on 2009-10-14 02:09
Frederick Cheung wrote:
> On Oct 13, 9:32�pm, Duane Morin <rails-mailing-l...@andreas-s.net>
> wrote:
>>
>> where it's generating a NULL for my non-specified field, and that's
>> breaking the constraint. �To the best of my ability to figure it out,
>> the code that does the MailQueueItem.create has not in any way changed.
>>
>> I'm hoping that somebody can say "Yes, version X of ActiveRecord now
>> behaves differently in that circumstance..."
>>
> 
> I do remember some changes on how defaults on text columns on mysql
> are handled. Does that sounds relevant ? Did your version of
> activerecord change ? (if so from what to what ?)
> 
> Fred

It appears that there was a mysql upgrade (5.0.45 to 5.0.77) on the day 
in question, but IT tells me no rails or gem updates.  But it doesn't 
seem like a mysql thing, as the insert statements would be generated on 
the Ruby side?

It's a mystery :(.  I can fix it easily enough (I'm explicitly setting 
all my fields now) but still, it'd be nice to know why it suddenly 
changed.
Posted by Frederick Cheung (Guest)
on 2009-10-14 22:16
(Received via mailing list)
On Oct 14, 1:09 am, Duane Morin <rails-mailing-l...@andreas-s.net>
wrote:
> It appears that there was a mysql upgrade (5.0.45 to 5.0.77) on the day
> in question, but IT tells me no rails or gem updates.  But it doesn't
> seem like a mysql thing, as the insert statements would be generated on
> the Ruby side?
>
I think this was a change in how show columns reported defaults - it's
mentionned in the release notes for 5.0.51 (http://dev.mysql.com/doc/
refman/5.0/en/news-5-0-51.html )

Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.