# Rake migrate ignores ':null =\> false' on mysql

**URL:** <https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702>\
**Category:** Rails\
**Created:** [December 16, 2005, 12:09am UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702 "2005-12-16T00:09:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [December 16, 2005, 12:09am UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702/1 "2005-12-16T00:09:26Z")

</div>

I wanted to add a login string column to an existing table  
=\> I created a migration :

…  
def self.up  
add\_column “members”, “login” , :string, :null =\> false  
end  
…

remark: it shouldn’t work, as the table is not empty (=\> ‘login’ would  
be null in the existing rows.)

problem:  
rake migrate didn’t complain and executed the migration, but didn’t add  
‘not null’ constraint.  
(I expected an SQL error here)

Bug? Feature? Limitation?

Alain

---

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [December 18, 2005, 12:44am UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702/2 "2005-12-18T00:44:04Z")

</div>

Lou  
\> it worked as advertised for me–migrations added NOT NULL.  
\>  
\> if you fail to specify a default you end up with ‘’ as the default  
value.  
\> I added field ‘eye\_color’ to my ‘users’ table using migrations,  
and got:

Was the table empty, before you ran the migration?

Alain

---

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [December 17, 2005, 10:58pm UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702/3 "2005-12-17T22:58:13Z")

</div>

it worked as advertised for me–migrations added NOT NULL.

if you fail to specify a default you end up with ‘’ as the default  
value.  
I added field ‘eye\_color’ to my ‘users’ table using migrations, and got:

mysql\> show create table users;

| users | CREATE TABLE `users` (  
`id` int(4) unsigned NOT NULL auto\_increment,  
`login` varchar(80) NOT NULL default ‘’,  
`salted_password` varchar(40) NOT NULL default ‘’,  
`email` varchar(60) NOT NULL default ‘’,  
`firstname` varchar(40) default NULL,  
`lastname` varchar(40) default NULL,  
`salt` varchar(40) NOT NULL default ‘’,  
`verified` int(11) default ‘0’,  
`role` varchar(40) default NULL,  
`security_token` varchar(40) default NULL,  
`token_expiry` datetime default NULL,  
`deleted` int(11) default ‘0’,  
`delete_after` datetime default NULL,  
`created_at` datetime default NULL,  
`logged_in_at` datetime default NULL,  
`updated_at` timestamp NOT NULL default CURRENT\_TIMESTAMP on update  
CURRENT\_TIMESTAMP,  
`eye_color` varchar(255) NOT NULL default ‘’,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

> script/about  
> About your application’s environment  
> Ruby version 1.8.4 (i386-cygwin)  
> RubyGems version 0.8.11  
> Rails version 1.0.0  
> Active Record version 1.13.2  
> Action Pack version 1.11.2  
> Action Web Service version 1.0.0  
> Action Mailer version 1.1.5  
> Active Support version 1.2.5  
> Application root /cygdrive/c/rails/relations  
> Environment development  
> Database adapter mysql

> mysql -u root -p -e “select version()”  
> Enter password: \*\*\*\*\*\*\*  
> ±--------------+  
> | version() |  
> ±--------------+  
> | 4.1.12-nt-max |  
> ±--------------+

---

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [December 18, 2005, 12:30pm UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702/4 "2005-12-18T12:30:33Z")

</div>

Alain R. wrote:

> Lou
> 
> > it worked as advertised for me–migrations added NOT NULL.
> > 
> > if you fail to specify a default you end up with ‘’ as the default  
> > value.  
> > I added field ‘eye\_color’ to my ‘users’ table using migrations, and  
> > got:
> 
> Was the table empty, before you ran the migration?

No, and defaults were applied to all records.

---

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [December 18, 2005, 1:12pm UTC](https://www.ruby-forum.com/t/rake-migrate-ignores-null-false-on-mysql/53702/5 "2005-12-18T13:12:37Z")

</div>

Lou V. \<vanek@…\> writes:

> > Was the table empty, before you ran the migration?
> 
> No, and defaults were applied to all records.
> 
> > Alain

The same thing happens with postgres adapter. I looked at the source and  
it’s most certainly a bug (at least one but two in my opinion). I  
actually  
“patched” it on my local installation  
[http://www.redhillconsulting.com.au/blogs/simon/archives/000298.html](http://www.redhillconsulting.com.au/blogs/simon/archives/000298.html)

Maybe you could do a similar thing until the problem is solved for good.

HTH,

Simon
