public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Change validates_uniqueness_of :case_sensitive option default back to true (from 
[9160]).  Love your database columns, don't LOWER them.  [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9248 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
Thu Apr 10 11:06:05 -0700 2008
commit  ed99dda174da439a0947cdabea3babf027c672ac
tree    4bb8781de7a4bf99d93d577446b2b74537332fe4
parent  c67e985994362290308073ed2793dd8e7f2a76db
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Change validates_uniqueness_of :case_sensitive option default back to true (from [9160]).  Love your database columns, don't LOWER them.  [rick]
0
+
0
 * Ensure that save on child object fails for invalid belongs_to association. Closes #11555. [rubyruy]
0
 
0
 * Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi]
...
603
604
605
606
 
607
608
609
...
613
614
615
616
 
617
618
619
...
603
604
605
 
606
607
608
609
...
613
614
615
 
616
617
618
619
0
@@ -603,7 +603,7 @@ module ActiveRecord
0
       # Configuration options:
0
       # * <tt>message</tt> - Specifies a custom error message (default is: "has already been taken")
0
       # * <tt>scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
0
-      # * <tt>case_sensitive</tt> - Looks for an exact match.  Ignored by non-text columns (false by default).
0
+      # * <tt>case_sensitive</tt> - Looks for an exact match.  Ignored by non-text columns (true by default).
0
       # * <tt>allow_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false)
0
       # * <tt>allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is: false)
0
       # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
0
@@ -613,7 +613,7 @@ module ActiveRecord
0
       #   not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }).  The
0
       #   method, proc or string should return or evaluate to a true or false value.
0
       def validates_uniqueness_of(*attr_names)
0
-        configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] }
0
+        configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true }
0
         configuration.update(attr_names.extract_options!)
0
 
0
         validates_each(attr_names,configuration) do |record, attr_name, value|

Comments

kemiller Sat Sep 20 13:51:14 -0700 2008

There’s an issue which has cropped up in this method, which is that there’s no need to LOWER the column in MySQL (which the comments even acknowledge), and yet MySQL is penalized in the case insensitive mode with unnecessary and utter destruction of its index utilitization. It wasn’t an issue before 2.1 because validates_uniqueness_of was effectively insensitive by default on MySQL (oops).

regabi Fri Oct 17 15:37:16 -0700 2008

I second kemiller’s comment. It’s a big issue on large tables with millions or rows getting scanned.

regabi Fri Oct 17 16:22:38 -0700 2008

As a temporary fix, I overrode validations.rb at line 627 with:

if value.nil? || (configuration[:case_sensitive] || !is_text_column) || self.connection.class.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
....
regabi Fri Oct 17 16:41:13 -0700 2008

oops, what I just wrote was wrong, I meant:

if value.nil? || (configuration[:case_sensitive] || !is_text_column) || self.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)

NZKoz Sun Oct 19 00:53:43 -0700 2008

Please open a thread on the core mailing list http://groups.google.com/group/rubyonrails-core rather than discussing this here, otherwise it’s likely to get forgotten and not be fixed in the next point release.