# MySQL and Default values

**URL:** <https://www.ruby-forum.com/t/mysql-and-default-values/70298>\
**Category:** Rails\
**Created:** [September 28, 2006, 12:40am UTC](https://www.ruby-forum.com/t/mysql-and-default-values/70298 "2006-09-28T00:40:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [September 28, 2006, 12:40am UTC](https://www.ruby-forum.com/t/mysql-and-default-values/70298/1 "2006-09-28T00:40:22Z")

</div>

I got my first taste today of dealing with the default values in MySQL.  
It seems that Rails loads the default values for fields based on the  
database (seems reasonable, specify in one place and let if flow  
through). The problem is that if I say NOT NULL and do not specify a  
default value, MySQL inserts a default value anyway. See

[http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html](http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html)

for all the details. Obviously Rails can’t fix this since it is an  
oddity with MySQL (that seems to be fixed in \>5.0.2). Rails has no way  
of knowing if the developer meant “not null with no default” or “not  
null with a default of an empty string”. So it seems to have taking the  
attitude of just doing what the database tells it. I guess the idea is  
that the developer can override this if the developer knows something  
more about the field. My problem is that I really don’t like invalid  
data sneaking through. But I don’t want to put a bunch over overrides  
all over my models either.

I would rather Rails assume I meant “not null with no default” when it  
sees “not null with a default of empty string” since it seems so rare  
that someone would actually want “not null with a default of empty  
string” compared to just “not null with no default”. For integers you  
have a much more common case of wanting “not null with a default of 0”  
but I still feel that it is less common than an integer field where you  
have “not null with no default”. So in effort to fix this problem I have  
developed the following code which I place in a file in my lib/  
directory and then require it from my environment.rb.

module ActiveRecord  
module ConnectionAdapters  
class MysqlColumn \< Column  
def default  
return @default if 50002 \<= sprintf(‘%02d%02d%02d’,  
\*ActiveRecord::Base.connection.send(:version)).to\_i  
return @default if name.to\_sym == :lock\_version  
case type  
when :integer, :float, :decimal  
@default == 0 ? nil : @default  
when :datetime, :timestamp  
@default == ‘0000-00-00 00:00:00’ ? nil : @default  
when :date  
@default == ‘0000-00-00’ ? nil : @default  
when :time  
@default == ‘00:00:00’ ? nil : @default  
when :text, :string  
@default == ‘’ ? nil : @default  
else  
@default  
end  
end  
end  
end

class Base  
class \<\< self  
alias db\_columns :columns  
def columns  
db\_columns.collect do |c|  
c.instance\_variable\_set ‘@default’,  
default\_for(c.name, c.default)  
c  
end  
end  
def default\_for(field, cur\_default)  
cur\_default  
end  
end  
end  
end

This code will try to guess what the developer meant only if we are  
running MySQL \< 5.0.2. It makes a special exception if the field is  
“lock\_version” since this is commonly an integer with a default of 0. In  
addition I have a hook that a model can override to correct my guessing  
if needed. Basically you override “default\_for” in your model. The  
method gets two arguments. The field name and the current guessed  
default (or the database default if no guessing took place). This allows  
a model to easily correct the database or my guessing if need be.

After setting up this code Rails will hopefully see the database as it  
was specified not as MySQL interpreted my schema.

I am thinking of packaging this code up as a plugin since it seems to  
work for me (just wrote it today so it is very alpha) but I was  
interested in getting thoughts from others to see if another solution  
might be more elegant and involve less guessing.

I look forward to feedback.

Eric
