Rails Migrations - Dropping default constraint on column
I’m working on a project for a client to convert a website from PHP to Ruby on Rails. The database is also changing from Microsoft SQL Server to MySQL, along with several schema redesigns.
One thing I wanted to do to our new db, was change a foreign key column from NOT NULL DEFAULT 0 to allow nulls and remove the default. Currently, the organizations table has a record with ID = 0 to signify a blank organization. Users that do not belong to an organization, have a foreign key to that Zero-record. Well, a better way to represent that is for the foreign key column in users to simply be NULL and ditch the Zero-record in the organizations table.
I have created a Migration class to handle the schema change. However, it looks like I have to drop to SQL as there is not Migration way of doing this. Here’s what it looks like:
execute 'ALTER TABLE users ALTER organization_id DROP DEFAULT'
I wanted to be able to handle this in the Migration-syntax, but I don’t think that’s possible. It would be nice to do something like:
change_column :users, :organization_id, :integer, :default => :null, :null => true