Migrations With Referential Integrity

Posted by Max Dunn Tue, 15 May 2007 18:11:00 GMT

Migrations don’t come with referential integrity directives, best as I can tell. There are some plugins but it appeared to me (ok, after only a brief review) that they were more focused on adding referential integrity for rails object hierarchies (:belongs_to etc.), so that seemed both overkill and insufficient for what I needed to do.

But it turns out that an “execute” statement in the migration lets you issue sql directives directly.

create_table "dvectors", :id => false, :force => true do |t|
  t.column "length",      :integer, :default => -1
  t.column "fvector",     :binary
  t.column "document_id", :integer, :default => -1
  t.column "total_size",  :integer, :default => 0
end
add_index "dvectors", ["document_id"], :name => "document_id" 
execute "alter table dvectors add constraint `dvectors_fk` " +
        "foreign key (`document_id`) references `documents` (`id`) on delete cascade"

(Submitted by Wido Menhardt)

Posted in  | no comments

Comments

(leave url/email »)

   Comment Markup Help Preview comment