<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Snow Giraffe Tech &#187; unique keys</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/unique-keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snowgiraffe.com/tech</link>
	<description>rails, rubies, and sometimes dolphins</description>
	<lastBuildDate>Mon, 07 Jun 2010 14:36:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails Devs for Data Integrity: How to gracefully handle database key violations</title>
		<link>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/</link>
		<comments>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 02:18:06 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>
		<category><![CDATA[foreign keys]]></category>
		<category><![CDATA[Rails Devs for Data Integrity]]></category>
		<category><![CDATA[unique keys]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=462</guid>
		<description><![CDATA[Join the club: Rails Devs for Data Integrity
Some ways to handle unique and foreign key violations database exceptions gracefully in rails.]]></description>
			<content:encoded><![CDATA[<div id="attachment_468" class="wp-caption alignright" style="width: 310px"><img class="size-medium wp-image-468" title="treehouse-fall-04-gif" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/treehouse-fall-04-gif-300x197.gif" alt="Join the club! Rails Devs for Data Integrity" width="300" height="197" /><p class="wp-caption-text">Join the club! Rails Devs for Data Integrity</p></div>
<p><span style="color: #ff6600;"><strong>I am a Rails Developer and I believe in data integrity. </strong></span>There I said it! And look, there are lots of people in the club. Jer on Rails had an<a href="http://jeronrails.blogspot.com/2008/01/you-should-use-foreign-key-constraints.html"> awesome post in support for foreign keys</a> a while back.</p>
<p>After my talk on supercharging ActiveRecord to behave in an enterprise environment at the<a href="http://www.snowgiraffe.com/tech/446/mysql-conf-09/"> MySQL conference</a>, a lot of folks were interested in how to get around the Rails <span class="inline_code">ActiveRecord</span> errors that appear when one starts deifying the Rails Way and uses foreign keys and unique indexes on the database. Well I thought about it and came up with not only a list but a yet another new plugin <a href="http://github.com/blythedunham/rails_devs_for_data_integrity" target="_blank">rails_devs_for_data_integrity</a> to help deal with some of the errors. So when someone tells you not to use foreign keys, don&#8217;t listen and<span style="color: #ff6600;"> join the Rails Devs for Data Integrity</span>! Here is how to have em, and still make pretty Rails apps.</p>
<p><span style="color: #ff6600;">Super list&#8230;.</span></p>
<p><span id="more-462"></span></p>
<h2>Unique Keys</h2>
<h3>Use validates_uniqueness_of</h3>
<p>validates_uniqueness_of introduces a query to check unique records before insert or update, but alone doesn&#8217;t guaranty uniqueness in a multi server/multi mongrel environment. By using it conjunction with a unique key on the database, you have proper rails error messages and your safety belt to retain data integrity. In most cases when dealing with a single instance (a single user&#8217;s user name for example), I can afford to eat the overhead of the rails validates_uniqueness_of query.</p>
<h3>Use on duplicate key update and ignore</h3>
<p><a href="http://github.com/zdennis/ar-extensions">ar-extensions 0.9.1</a> supports ON DUPLICATE KEY UPDATE and IGNORE for MySQL on import, save, and create (all inserts and updates). This is especially practical for import (bulk insert) where validating each record&#8217;s uniqueness would produce a lot of overhead.</p>
<h3>Catch and Handle the Duplicate Violation Exception</h3>
<p>I wrote a lot of custom code in models and controllers to catch MySQL duplicate key error (<span class="inline_code">ActiveRecord::StatementInvalid</span>) and handle them appropriately. Typically, I added a new error to ActiveRecord::Base.errors, which is then displayed nicely with <span class="inline_code">error_messages_for</span> in the view. Sometimes this was done often with a save_safe model method possibly aliased to save or a rescue on the controller action.</p>
<p>After MySQLConf I decided to write a little plugin <a href="http://github.com/blythedunham/rails_devs_for_data_integrity">rails_devs_for_data_integrity</a> to convert exceptions into ActiveRecord errors (like validations do) for tables with unique and foreign keys . Its still infantile (and without tests, gasp!) but if anyone likes to find bugs, hit me up on<a href="http://github.com/blythedunham/rails_devs_for_data_integrity/issues"> githubs new issue tracker</a>. If there is more than one unique key per table, you might want write some custom error handling methods.</p>
<pre>script/plugin install git://github.com/blythedunham/rails_devs_for_data_integrity.git</pre>
<pre class="ruby"> <span class="keyword">class </span><span class="class">User</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
   <span class="ident">handle_unique_key_violation</span>  <span class="symbol">:user_name</span><span class="punct">,</span> <span class="symbol">:message</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">is taken"
   handle_foreign_key_violation :primary_email_id, :message =&gt; </span><span class="punct">'</span><span class="ident">is</span> <span class="keyword">not</span> <span class="ident">available</span><span class="punct">'</span><span class="string">
 end<span class="normal">

</span></span></pre>
<p>Will write ActiveRecord errors instead of nasty MySQL errors:</p>
<pre> &gt;&gt; user.errors.on(:user_name)
 =&gt; "association does not exist."

 &gt;&gt; user.errors.on(:primary_email_id)
 =&gt; "is a duplicate."</pre>
<h3>Overwrite <em>rescue_action_in_public</em> in ApplicationController</h3>
<p>Its a good idea to either send the user to a pretty 404 static generic error page (fast) or write some custom code in rescue_action_in_public. One idea is to create <span class="inline_code">DisplayableException</span> and subclass any exceptions where the text can be displayed to the user. If this is thrown, show <span class="inline_code">exeception.to_s</span>, if not show a generic error.</p>
<pre class="ruby"><span class="keyword">def </span><span class="method">rescues_action_in_public</span><span class="punct">(</span><span class="ident">exception</span><span class="punct">)</span>
  <span class="attribute">@message</span> <span class="punct">=</span> <span class="keyword">if</span> <span class="ident">exception</span><span class="punct">.</span><span class="ident">is_a?</span><span class="punct">(</span><span class="constant">DisplayableException</span><span class="punct">)</span>
    <span class="ident">exception</span><span class="punct">.</span><span class="ident">to_s</span>
  <span class="keyword">else</span>
    <span class="punct">"</span><span class="string">Sorry but an error occurred. Please contact your mommy.</span><span class="punct">"</span>
  <span class="keyword">end</span>
  <span class="ident">render</span> <span class="symbol">:action</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">error_page</span><span class="punct">'</span>
<span class="keyword">end</span></pre>
<h2>Foreign Keys</h2>
<p>I have been using the <a href="http://agilewebdevelopment.com/plugins/owner/89">Redhills foreign key migration plugin</a> for a long time and haven&#8217;t had too much trouble with foreign key violations.</p>
<h3>Trouble Deleting Records</h3>
<p>At one point <span class="inline_code">ON DELETE</span> was not specified to <span class="inline_code">SET NULL</span> or <span class="inline_code">CASCADE</span> on many dependent columns and there was trouble deleting rows. Reindexing the database with this option was the solution. Similarly,  one could use <span class="inline_code">ON UPDATE</span> on foreign key indexes.  However, due to the way ActiveRecord works and doesn&#8217;t really update id columns, I haven&#8217;t had any issues.</p>
<h3>Foreign Key Validation Errors</h3>
<p>This has happened so seldom that I never worried about it. That&#8217;s what the refresh button is for! Perhaps its because most users have their own data and aren&#8217;t really modifying and deleting simultaneously. However, sure this can happen and it throws an <span class="inline_code">ActiveRecord::StatementInvalid</span> exception as with unique key violations. I would recommend the same approach as for unique keys: catch and handle the exception. I added foreign key support to the <a href="http://github.com/blythedunham/rails_devs_for_data_integrity" target="_blank">rails_devs_for_data_integrity plugin</a> experiment. Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
