mysql5 defaults, rails and not null text columns

Hi all,

Here's a funny situation:

MySQL 5 does not support default values for columns of type text (and binary).

MySQL 5 will throw an error if you try to insert a null value into a column that is NOT NULL.

Rails knows that MySQL wrongly reports defaults as '' for NOT NULL columns with no default specified. Rails also knows which column types don't allow default values (text, binary).

So when rails is dealing with a TEXT NOT NULL column, it'll know it doesn't support a default value, and it'll understand the reported default value of '' is fake. It'll assume no default.

So here's the issue: when initializing a new record, rails will set the attribute to nil. Then, when saving it, MySQL will throw because you tried inserting NULL into a NOT NULL column.

My suggestion is simple:

In the specific case that a column disallows default and is NOT NULL, report the default as MySQL's implicit default, ''. The attribute should be initialized to it, and schema dumper should dump the column with said default value.

The reasoning is that effectively, '' IS the default value for a TEXT NOT NULL column.

Makes sense?

Calo,

I think this is the perfect solution for the problem.

-- Ferdinand

Makes sense?

This makes sense to me, if you can whip up a patch and have it reviewed we should figure out if it breaks anything else.

http://dev.rubyonrails.org/ticket/9324

Hey Koz, can you take a look at the patch?

http://dev.rubyonrails.org/ticket/9324