views:

807

answers:

1

Hi there,

I am trying to run a migration on an existing database to change the column name on a table. When I run the migration, I get an error stating that Blob/Text fields cannot have a default value. The column in question is a text column, with a non-null attribute, but no default value.

The migration that Rails attempts is:

ALTER TABLE xxxxx CHANGE abcd ABCD text DEFAULT '' NOT NULL

Now, I haven't asked the migration to change the column type, I have only asked it to rename the column, so why is the migration trying to do anything to the column type?

I have Googled the issue, and haven't come up with an explanation or workaround.

Any help appreciated.

Vikram

A: 

There does seem to be a longstanding unresolved ticket on this issue, as described here:

rails bug report

Rails' default behavior is to make columns which are NULL, since this prevents false positives on presence checks, etc, when translating blank strings back into Ruby. Any chance you can work around this by redefining your text column to work with NULL values in the mySQL console?

EDIT

You can do this in your migration file, it's not the Rails way but it's a lot nicer than sending an email to everyone to change their local copies:

MyModel.connection.execute "ALTER TABLE xxxxx CHANGE abcd ABCD text DEFAULT NULL"
austinfromboston
Arrghh the pain!I was trying to do the right thing by NOT going out of the Rails Migration and documenting everything properly so that others could follow, and could just run rake db:migrate. This puts in an extra step external to Rails.Thanks for pointing out the bug report though. I am not sure why I didn't find it.
Vikram Goyal
Thanks, yes, using the execute to do a manual update is much better than external updates.
Vikram Goyal