<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Eschew Obfuscation: Migrations With Referential Integrity</title>
    <link>http://blog.maxdunn.com/articles/2007/05/15/migrations-with-referential-integrity</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Max Dunn's Personal Blog</description>
    <item>
      <title>Migrations With Referential Integrity</title>
      <description>&lt;p&gt;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.&lt;/p&gt;


	&lt;p&gt;But it turns out that an “execute” statement in the migration lets you issue sql directives directly.&lt;/p&gt;


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


	&lt;p&gt;(Submitted by Wido Menhardt)&lt;/p&gt;</description>
      <pubDate>Tue, 15 May 2007 11:11:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:77732030-df55-4b1c-9fa0-12cc38849a21</guid>
      <author>Max Dunn</author>
      <link>http://blog.maxdunn.com/articles/2007/05/15/migrations-with-referential-integrity</link>
      <category>Ruby on Rails</category>
    </item>
  </channel>
</rss>
