22
Jan
2009
Jan
2009
0
Constraints in Rails Migrations
I decided to try a rails project again… something I do quite frequently after I’ve completely forgotten everything I’ve learned.
One thing I quickly remembered is that there is no easy way in a migration to create constraints, like a foreign key. You have to do some exec silliness with an actual query.
After some quick searching, I found this plugin: http://rubyforge.org/projects/mig-constraints
It gives you the ability to do this to specify some common constraints in the migration.
References
table.column :user_id, :integer, :references => :users
Unique
table.column :username, :string, :unique => true
Check
table.column :unit_price, :double, :check => '> 0'
Very helpful. Sadly, like all Rails plugins, needs to be installed per project.
Category: Programming
Tags: Ruby on Rails