"Invalid use of NULL Value", MySQL and RoR Migrations
Today I was working with a Ruby on Rails application, configured to use MySQL 5.6.10, that kept failing to perform a column-modifying migration:
class SomeModel < ActiveRecord::Migration def up change_column(:some_models, :some_value, :integer, default: 0, null: false) end def down end end
The error message, from the Mysql12 adapter, was "Invalid use of NULL value"
.
Using the test database I could change the schema manually, from the MySQL prompt. But I couldn't do so in the development database. As is typical, the test database held no records. That should have been my clue.
Eventually I realized that in the development database the "some_models" table contained many records, at least some of which held NULLs for "some_value".
After performing a manual update:
update some_models set some_value = 0 where some_value is null;
I was able to run the migration.
[Edited 20130822 for typos]