Showing posts with label custom data type. Show all posts
Showing posts with label custom data type. Show all posts

Wednesday, March 12, 2008

Assigning custom data types to new columns during migration, part 2

In my previous article, Assigning custom data types to new columns during migrations, I showed how you can create columns with custom data types during a database migration. Simply specify :"data type", instead of :integer, :string, :binary, etc. An example would be :"smallint", since there is no direct way to create a MySQL smallint during a Rails migration.

However, I recently found that this doesn't always work. It works fine if you are creating a new table in your migration. But this does NOT work if you are adding or modifying a column in an already existing table, you get the error:
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Has anyone else run in to this problem? It's annoying to have to construct SQL statements to add columns, after all that's what migrations are supposed to get rid of.

Monday, November 26, 2007

Assigning custom data types to new columns during migration

The set of data types that you can assign to a new column during a database migration is pretty limited. However, it's easy to set a column to a data type specific to the database that you're using. Instead of specifying :string, :integer, etc for the data type, specify :"data type". As an example, if you have MySQL and you want to add an unsigned integer, there's no way to specify a :integer to be unsigned. But you can do this:
add_column :table, :newcolumn, :"int (10) unsigned"