<?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>H3RALD: Simply on Rails - Part 2: Database Design</title>
    <link>http://www.h3rald.com/blog/simply-on-rails-2-database-design</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Fabio Cevasco's Writings</description>
    <item>
      <title>Simply on Rails - Part 2: Database Design</title>
      <description>&lt;p&gt;This week I attended a course for work on how to &lt;em&gt;Implement Databases with Microsoft &lt;span class="caps"&gt;SQL&lt;/span&gt; Server 2005&lt;/em&gt;. An interesting course indeed, which made me realize how feature-rich Bill&amp;#8217;s product is, compared to the Open Source alternatives like MySQL. It also made me realize how nice it is to implement database-related logic (read: Models) using a &lt;em&gt;proper&lt;/em&gt; programming language rather than using triggers, stored procedures, functions and other goodies offered by Transact-SQL.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s all a matter of taste and of necessities: using &lt;span class="caps"&gt;MS SQL&lt;/span&gt; Server for one of my website is simply not going to happen anytime soon, and I&amp;#8217;m more than happy to have a database which can be used &lt;em&gt;just&lt;/em&gt; as a database and a programming language (Ruby, in this case) which can do wonders, rather than a procedural-only surrogate.&lt;/p&gt;


	&lt;p&gt;Anyhow, back to our weekly series. After creating a &lt;a href="/blog/simply-on-rails-1-concepts-map"&gt;concept map&lt;/a&gt;, it&amp;#8217;s time of &lt;em&gt;get real&lt;/em&gt; and try to figure out a database architecture.&lt;/p&gt;


	&lt;p&gt;The tool of choice this week is obviously the widely popular &lt;a href="http://fabforce.net/dbdesigner4/"&gt;DbDesigner 4&lt;/a&gt;. It&amp;#8217;s free, it&amp;#8217;s easy to use, and the results are pretty enough. There:&lt;/p&gt;


	&lt;p&gt;&lt;a href="/files/italysimply_database-architecture.png"&gt;&lt;img src="/files/italysimply_database-architecture_thumb.png" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s amazing how a relatively simple concept map can lead to such a complex database architecture, isn&amp;#8217;t it?
Well, it&amp;#8217;s normal. One of the reasons of this is that I totally forgot about geographical information about the houses which will be featured on the site, or better, I thought about it as a &lt;em&gt;strings&lt;/em&gt; typed in by the administrators, whereas it would be much better having dropdown boxes.&lt;/p&gt;


	&lt;p&gt;Countries, regions, privinces, areas and cities will be added to the database only once, rather than having to type them in every time a house is added. Obvious, but this lead to five tables more and nine (!) relationships more.&lt;/p&gt;


	&lt;p&gt;The other reason of why the number of tables is higher than the number of entities in the domain model is that I decided &lt;em&gt;not&lt;/em&gt; to use the &lt;span class="caps"&gt;ENUM&lt;/span&gt; type. Firstly because &lt;a href="http://wiki.rubyonrails.org/rails/pages/HowtoUseSetAndEnumColumns"&gt;it&amp;#8217;s not handled very well by Rails&lt;/a&gt;
 and also because there&amp;#8217;s &lt;a href="http://blog.arabx.com.au/?p=87"&gt;a number of reasons&lt;/a&gt; why ENUMs should not be used.&lt;/p&gt;


	&lt;p&gt;The only problem now is that whenever I load a house, I&amp;#8217;ll have to get data from a lot of tables at once (and this means a lot of joins underneath the model layer) or &amp;#8211; worse &amp;#8211; a lot of queries in case I decide to load related data &amp;#8220;on the fly&amp;#8221;. It looks like I&amp;#8217;ll have to do a bit of &lt;a href="http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes"&gt;piggy-backing&lt;/a&gt; here and there. &lt;a href="http://railsexpress.de/blog/articles/2006/05/29/simpler-piggy-backing"&gt;Someone&lt;/a&gt; already thought about a way of doing this in a more &amp;#8220;Model-friendly&amp;#8221; way. Perhaps I&amp;#8217;ll give it a shot.&lt;/p&gt;</description>
      <pubDate>Sat, 14 Jul 2007 11:27:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:9da91c98-b776-4c68-8228-fd009ca26c2d</guid>
      <author>h3rald</author>
      <link>http://www.h3rald.com/blog/simply-on-rails-2-database-design</link>
      <category>Blog</category>
      <category>Rails</category>
      <category>databases</category>
      <trackback:ping>http://www.h3rald.com/trackback/entries/107</trackback:ping>
    </item>
  </channel>
</rss>
