Rails: Creating Database Migrations

Posted on January 26, 2007
Filed Under /dev/ruby | 128 views |

Part of the sweet tool suite in Rails is ActiveRecord’s Migration, a database-agnostic mechanism for defining the application’s database structure as well as any changes that need to be made to the database over the life of the project.

The Rails wiki entry for Using Migrations.

The supported datatypes for database columns (note that these don’t map 1-1 to MySQL or PostgreSQL or SQLite for a reason: it’s all agnostic-like):

:integer
:float
:datetime
:date
:timestamp
:time
:text
:string
:binary
:boolean

Handy considerations to keep in mind from the wiki page:

Valid column options are limit, null (i.e. ” :null => false” implies NOT NULL), and default (to specify default values)

An ID column(primary key) will be created automatically

If you attempt to run rake migrate and it fails, what should be attempted next?
rake migrate -t

How do I create a multiple-column index?
add_index :cars, [:license_plate, :state], :unique

Adding an index:

add_index(table_name, column_names, index_type, index_name)

ie: add_index :user_accounts, [:username, :display_name], :unique => true, :name => ‘name’

(Original pointer to this info via Solutions Log)

Comments

Leave a Reply