<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: validates_uniqueness_of does not guarantee uniqueness</title>
	<atom:link href="http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/feed/" rel="self" type="application/rss+xml" />
	<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/</link>
	<description>Ecchi nanowa ikenai to omoimasu</description>
	<lastBuildDate>Tue, 03 Jan 2012 16:14:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Hongli</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-10101</link>
		<dc:creator>Hongli</dc:creator>
		<pubDate>Fri, 08 Apr 2011 08:34:54 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-10101</guid>
		<description>I see it as a limitation in SQL. It has no explicit support for &quot;upsert&quot; commands (a name for the concept &quot;update or insert if not exist&quot;, which is what this use case is all about). When those two SELECTs in the above example are being executed, it is impossible to know beforehand whether they are regular SELECTs or whether they&#039;re part of an upsert command. Databases try to optimize for the highest concurrency, therefore they treat them like regular SELECTs and don&#039;t isolate.

Running SELECT ... FOR UPDATE on the other hand does work, and it signals the database that an upsert is imminent. However this is a non-standard MySQL extension.</description>
		<content:encoded><![CDATA[<p>I see it as a limitation in SQL. It has no explicit support for &#8220;upsert&#8221; commands (a name for the concept &#8220;update or insert if not exist&#8221;, which is what this use case is all about). When those two SELECTs in the above example are being executed, it is impossible to know beforehand whether they are regular SELECTs or whether they&#8217;re part of an upsert command. Databases try to optimize for the highest concurrency, therefore they treat them like regular SELECTs and don&#8217;t isolate.</p>
<p>Running SELECT &#8230; FOR UPDATE on the other hand does work, and it signals the database that an upsert is imminent. However this is a non-standard MySQL extension.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Russell</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-10100</link>
		<dc:creator>Paul Russell</dc:creator>
		<pubDate>Fri, 08 Apr 2011 05:44:06 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-10100</guid>
		<description>Can anyone explain /why/ setting the transaction isolation level to serialised doesn&#039;t help here? I&#039;m not really interested in a &#039;try it yourself&#039; response, I&#039;m interested in an explanation that doesn&#039;t involve the database violating the rules of serialised transactions (i.e. that it MUST yield identical results to as if the transactions had been executed separately). The information in this post has propagated far and wide (including here, which is where I picked it up: http://stackoverflow.com/questions/5577562/rails-maximum-number-of-associated-records/5580367#5580367) as a result of being added to the rails docs, and I just can&#039;t get my head around how this can be true! Help! :)</description>
		<content:encoded><![CDATA[<p>Can anyone explain /why/ setting the transaction isolation level to serialised doesn&#8217;t help here? I&#8217;m not really interested in a &#8216;try it yourself&#8217; response, I&#8217;m interested in an explanation that doesn&#8217;t involve the database violating the rules of serialised transactions (i.e. that it MUST yield identical results to as if the transactions had been executed separately). The information in this post has propagated far and wide (including here, which is where I picked it up: <a href="http://stackoverflow.com/questions/5577562/rails-maximum-number-of-associated-records/5580367#5580367" rel="nofollow">http://stackoverflow.com/questions/5577562/rails-maximum-number-of-associated-records/5580367#5580367</a>) as a result of being added to the rails docs, and I just can&#8217;t get my head around how this can be true! Help! <img src='http://izumi.plan99.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eno</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9882</link>
		<dc:creator>eno</dc:creator>
		<pubDate>Wed, 04 Mar 2009 17:29:09 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9882</guid>
		<description>Hell, no, &quot;A good work around is to use a SELECT FOR UPDATE wrapped in a transaction, which will lock the database.&quot;, is a workaround, and a nonworking at that, because

 * it breaks any login based on transactions (unless you run a database that supports nested transactions) and
 * because it doesn&#039;t work anyways because, erm, it just doesn&#039;t work. Just try it manually in two MySQL consoles in parallel.

what *does* work instead is locking the database, see (if you are interested) the related posts here: http://1rad.wordpress.com/2009/02/23/0x17-atomic-science-revisited/</description>
		<content:encoded><![CDATA[<p>Hell, no, &#8220;A good work around is to use a SELECT FOR UPDATE wrapped in a transaction, which will lock the database.&#8221;, is a workaround, and a nonworking at that, because</p>
<p> * it breaks any login based on transactions (unless you run a database that supports nested transactions) and<br />
 * because it doesn&#8217;t work anyways because, erm, it just doesn&#8217;t work. Just try it manually in two MySQL consoles in parallel.</p>
<p>what *does* work instead is locking the database, see (if you are interested) the related posts here: <a href="http://1rad.wordpress.com/2009/02/23/0x17-atomic-science-revisited/" rel="nofollow">http://1rad.wordpress.com/2009/02/23/0&#215;17-atomic-science-revisited/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hongli</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9637</link>
		<dc:creator>Hongli</dc:creator>
		<pubDate>Tue, 25 Nov 2008 23:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9637</guid>
		<description>Fernando, I never claimed I discovered anything. I merely documented it.</description>
		<content:encoded><![CDATA[<p>Fernando, I never claimed I discovered anything. I merely documented it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fernando</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9636</link>
		<dc:creator>fernando</dc:creator>
		<pubDate>Tue, 25 Nov 2008 23:55:13 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9636</guid>
		<description>You didn&#039;t discover anything at all. This race condition is known for a long time. I posted something about it 6 months ago at the following link: http://www.ruby-forum.com/topic/154078

A good work around is to use a SELECT FOR UPDATE wrapped in a transaction, which will lock the database.</description>
		<content:encoded><![CDATA[<p>You didn&#8217;t discover anything at all. This race condition is known for a long time. I posted something about it 6 months ago at the following link: <a href="http://www.ruby-forum.com/topic/154078" rel="nofollow">http://www.ruby-forum.com/topic/154078</a></p>
<p>A good work around is to use a SELECT FOR UPDATE wrapped in a transaction, which will lock the database.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik N</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9562</link>
		<dc:creator>Henrik N</dc:creator>
		<pubDate>Wed, 29 Oct 2008 12:52:05 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9562</guid>
		<description>This is how I prevent double submits client-side: http://henrik.nyh.se/2008/07/jquery-double-submission</description>
		<content:encoded><![CDATA[<p>This is how I prevent double submits client-side: <a href="http://henrik.nyh.se/2008/07/jquery-double-submission" rel="nofollow">http://henrik.nyh.se/2008/07/jquery-double-submission</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9510</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Wed, 24 Sep 2008 18:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9510</guid>
		<description>Typical Rubyist: If you read my comment, you would see I recommend client-side prevention _in addition to_ backend constraints. On many applications, the client-side component will prevent the vast majority of these problems, making for a much better visitor experience.

As a general rule, it&#039;s a good idea to re-read a comment before you flat out insult it.</description>
		<content:encoded><![CDATA[<p>Typical Rubyist: If you read my comment, you would see I recommend client-side prevention _in addition to_ backend constraints. On many applications, the client-side component will prevent the vast majority of these problems, making for a much better visitor experience.</p>
<p>As a general rule, it&#8217;s a good idea to re-read a comment before you flat out insult it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hongli</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9507</link>
		<dc:creator>Hongli</dc:creator>
		<pubDate>Tue, 23 Sep 2008 16:58:11 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9507</guid>
		<description>Typical Rubyist: it isn&#039;t such a bad idea. Of course it shouldn&#039;t be the *only* protection, but when implemented it can drastically reduce the chance of duplicate inserts.</description>
		<content:encoded><![CDATA[<p>Typical Rubyist: it isn&#8217;t such a bad idea. Of course it shouldn&#8217;t be the *only* protection, but when implemented it can drastically reduce the chance of duplicate inserts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Typical Rubyist</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9506</link>
		<dc:creator>Typical Rubyist</dc:creator>
		<pubDate>Tue, 23 Sep 2008 15:40:46 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9506</guid>
		<description>Hahaha use JS to disable doubleclicks? Are you serious? If you rely on client, you will lose.</description>
		<content:encoded><![CDATA[<p>Hahaha use JS to disable doubleclicks? Are you serious? If you rely on client, you will lose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hongli</title>
		<link>http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/comment-page-1/#comment-9500</link>
		<dc:creator>Hongli</dc:creator>
		<pubDate>Mon, 22 Sep 2008 12:36:02 +0000</pubDate>
		<guid isPermaLink="false">http://izumi.plan99.net/blog/index.php/2008/09/20/validates_uniqueness_of-does-not-guarantee-uniqueness/#comment-9500</guid>
		<description>Fjan, transactions doesn&#039;t help you here, even if you use serializable. Try it out for yourself. Unless user 2&#039;s first SELECT statement blocks until user 1&#039;s transaction has been completed (which PostgreSQL doesn&#039;t do), transactions won&#039;t prevent the duplicate insertion.</description>
		<content:encoded><![CDATA[<p>Fjan, transactions doesn&#8217;t help you here, even if you use serializable. Try it out for yourself. Unless user 2&#8242;s first SELECT statement blocks until user 1&#8242;s transaction has been completed (which PostgreSQL doesn&#8217;t do), transactions won&#8217;t prevent the duplicate insertion.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

