ActiveRecord failing with a default field value of blank

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

On Oct 13, 9:32 pm, Duane M. [email protected]
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

On Tue, Oct 13, 2009 at 3:32 PM, Duane M.
[email protected] 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 D.
http://destiney.com/

Frederick C. wrote:

On Oct 13, 9:32�pm, Duane M. [email protected]
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.

On Oct 14, 1:09 am, Duane M. [email protected]
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