Rails rename_column migration issue
-
13-09-2019 - |
Question
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
Solution
There does seem to be a longstanding unresolved ticket on this issue, as described here:
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"