<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Wolfmans Howlings: Create Rails ActiveRecord Models from DDL</title>
    <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A programmers Blog about Ruby, Rails and a few other issues</description>
    <item>
      <title>Create Rails ActiveRecord Models from DDL</title>
      <description>&lt;p&gt;I had a database with about 60 tables in it, most where simple lookup
tables with simple has_many and belongs_to relationships, and I didn't
want to manually create all the models, with the associations by hand.
I googled around and came across 
&lt;a href="http://www.billkatz.com/rails_dbmodel"&gt;Bill Katz's dbmodel&lt;/a&gt;, 
which takes the output of dbdesigner and creates the models with the
relationships.  However I already had the Databases and schema setup,
and I didn't have (and couldn't find) a copy of dbdesigner to use. So I
hacked Bills dbmodel to read a DDL file that was created from the
command rake db:structure:dump, as I was using Postgresql this file had
all the relevant relationship info in it plus a bit extra.&lt;/p&gt;

&lt;p&gt;I added the ability to create the relationships that had non standard
table names and foreign keys, and also added some validations to the
created models.&lt;/p&gt;

&lt;p&gt;By default all relationships are created as has_many (and belongs_to),
and tables can have multiple belongs_to referencing the same table so
long as the foreign keys are different. Also models are created for
every table found in the DDL.&lt;/p&gt;

&lt;p&gt;I also added an yaml file as an override so you can specify habtm and
has_one relationships too.&lt;/p&gt;

&lt;p&gt;There are two files to this modification, a hacked version of the
original &lt;a href="http://dnload.e4net.com/dbmodel.rb"&gt;dbmodel.rb&lt;/a&gt; and a new file which encapsulates the parsing of
the DDL file. The main changes to the original dbmodel.rb consists of
removing the parsing of the original xml file and reading the changes
from a modified hash of the tables and associations.&lt;/p&gt;

&lt;p&gt;The new &lt;a href="http://dnload.e4net.com/ddl.rb"&gt;ddl.rb&lt;/a&gt; file handles the parsing of the DDL file, and building
a hash of data for the tables, with their associations and
validations. It also reads the YAML file that overides the association
types.&lt;/p&gt;

&lt;p&gt;You can get a zip of the two files from this link &lt;a href="http://dnload.e4net.com/ddl2model.zip"&gt;ddl2model.zip&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both files need to be in the same directory and it is run from the
command line, the yaml override being in the same directory as the
dbmodel.rb script. The ddl file to be processed is given on the
command line.&lt;/p&gt;

&lt;p&gt;The YAML file &lt;code&gt;assocs.yml&lt;/code&gt; is used to tell the ddl.rb script about
relations that are anything other than has_many, so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;extable1:
   - {:assoc: has_one, :ref: extable2, :column: extable1_id}
   - {:assoc: has_one, :ref: extable3, :column: extable1_id}
   - {:assoc: habtm, :ref: extable1_extable2, :column: extable1_id}
extable2:
   - {:assoc: habtm, :ref: extable1_extable2, :column: extable2_id}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Tells the parser that the model for a table named extable1 should create a has_one
relation for extable2 using the foreign key extable1_id. Ditto for extable3.&lt;/p&gt;

&lt;p&gt;It also specifies that the model for extable2 has a habtm relationship
using the join table extable1_extable2 using foreign key extable1_id.
Note that the habtm override needs to be specified for both tables, in
this case extable2 also has a habtm override.&lt;/p&gt;

&lt;p&gt;You can also ignore a table with this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;table3: []
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;will ignore the table called table3.&lt;/p&gt;

&lt;p&gt;Lastly the generator will add validate_presence_of based on any NOT
NULL constraints found on the column DDL.&lt;/p&gt;

&lt;h3&gt;Update&lt;/h3&gt;

&lt;p&gt;A better solution to this problem has been provided here &lt;a href="http://db-discovery.rubyforge.org/"&gt;http://db-discovery.rubyforge.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://technorati.com/tag/activerecord+model+generate" rel="tag"&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 24 May 2006 23:20:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:02a5db688ee162b3c20a60fad6d5fe78</guid>
      <author>Jim Morris</author>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl</link>
      <category>Rails</category>
      <category>Ruby</category>
      <category>rails</category>
      <category>ddl</category>
      <category>activerecord</category>
      <trackback:ping>http://blog.wolfman.com/articles/trackback/15</trackback:ping>
    </item>
    <item>
      <title>"Create Rails ActiveRecord Models from DDL" by wolfman</title>
      <description>&lt;p&gt;Probably best to file a change request on the Typo blog, I agree having the date show up after say 30 days is probably a good idea, as for the _ italic problem that is just the textile markup, not much I can do about that other than disallow markup in comments, simply putting the variable with underscrores between backtics solves the problem...
&lt;code&gt;this_is_not_italized&lt;/code&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 18 Oct 2006 09:40:27 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:943d6961-8d07-4d12-b766-478567b47f9d</guid>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl#comment-1</link>
    </item>
    <item>
      <title>"Create Rails ActiveRecord Models from DDL" by hank</title>
      <description>&lt;p&gt;also: with so many things in Rails being delineated by underscores, it seems ridiculous to be using blogging software that converts a word surrounded by underscores into italics.&lt;/p&gt;

&lt;p&gt;Usability is king.&lt;/p&gt;

&lt;p&gt;foo_on_you
&lt;em&gt;foo&lt;/em&gt;on_you
what&lt;em&gt;the&lt;/em&gt;hell&lt;em&gt;is_going&lt;/em&gt;on_here?&lt;/p&gt;</description>
      <pubDate>Wed, 18 Oct 2006 05:48:36 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:ff0f12df-ae97-41ec-87c3-a398e1db5436</guid>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl#comment-10</link>
    </item>
    <item>
      <title>"Create Rails ActiveRecord Models from DDL" by hank</title>
      <description>&lt;p&gt;"wolfman  said 145 days later:"&lt;/p&gt;

&lt;p&gt;This new "so many days later" craze is useful in the first few days following an original post, but as time goes on it becomes less and less useful.  "time&lt;em&gt;from&lt;/em&gt;now_in_words" is cool and all, but in the end it would be nice to tell at a glance whether or not a comment is 'stale'.  As it stands, I need to scroll back up to the top, check the original post date, and add 145 days to that to figure out how long ago the comments were posted.  This could be fixed by simply adding the boring old pre-Web2.0 date to the comment header.&lt;/p&gt;</description>
      <pubDate>Wed, 18 Oct 2006 05:45:18 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:0b23143f-37f9-40ee-b7dc-b8c1dd335383</guid>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl#comment-24</link>
    </item>
    <item>
      <title>"Create Rails ActiveRecord Models from DDL" by wolfman</title>
      <description>&lt;p&gt;What database are you using? Right now it support postgresql, but I made a small update that will work with mysql, except for the validations.
If you send me the development_structure.sql I can make sure I can parse it and send you any changes to ddl.rb. It sounds like ddl.rb is not reading the DDL properly you can check this by running ddl.rb directly on the .sql file, it should print out a list of tables it finds.&lt;/p&gt;</description>
      <pubDate>Tue, 17 Oct 2006 13:13:13 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:738e30e3-696c-434a-b656-d608ac6d9581</guid>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl#comment-30</link>
    </item>
    <item>
      <title>"Create Rails ActiveRecord Models from DDL" by Chris</title>
      <description>&lt;p&gt;Hey Wolfman,
I have been looking for a way to automatically create the belongs_to relationships in the model, and have thankfully googled this post. &lt;/p&gt;

&lt;p&gt;I am having problems getting the model to update. I have done exactly what I think I need to do, &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;rake db:structure:dump&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;which creates \db\development_structure.sql&lt;/p&gt;

&lt;p&gt;then I am running&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;ruby db\dbmodel.rb db\development_structure.sql
    =&gt;Reading the DDL (db\development_structure.sql)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;then I look at the model, but nothing is changed. &lt;/p&gt;

&lt;p&gt;Am I missing something?&lt;/p&gt;</description>
      <pubDate>Tue, 17 Oct 2006 08:39:56 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:34e062b2-8a60-42f5-a521-b669c275ed9a</guid>
      <link>http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl#comment-23</link>
    </item>
  </channel>
</rss>
