Permalink
Browse files

Fix validates_uniqueness_of for SQL keywords [#23 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent 0dabb5b commit 4cc594bd708df1ec67b61833493198ab0009c627 @maccman maccman committed with lifo May 4, 2008
View
2 activerecord/lib/active_record/validations.rb
@@ -640,7 +640,7 @@ def validates_uniqueness_of(*attr_names)
results = finder_class.with_exclusive_scope do
connection.select_all(
construct_finder_sql(
- :select => "#{attr_name}",
+ :select => "#{connection.quote_column_name(attr_name)}",
:from => "#{finder_class.quoted_table_name}",
:conditions => [condition_sql, *condition_params]
)
View
8 activerecord/test/cases/validations_test.rb
@@ -5,6 +5,7 @@
require 'models/person'
require 'models/developer'
require 'models/warehouse_thing'
+require 'models/guid'
# The following methods in Topic are used in test_conditional_validation_*
class Topic
@@ -493,6 +494,13 @@ def test_validates_uniqueness_inside_with_scope
end
end
+ def test_validate_uniqueness_with_columns_which_are_sql_keywords
+ Guid.validates_uniqueness_of :key
+ g = Guid.new
+ g.key = "foo"
+ assert_nothing_raised { !g.valid? }
+ end
+
def test_validate_straight_inheritance_uniqueness
w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork")
assert w1.valid?, "Saving w1"
View
2 activerecord/test/models/guid.rb
@@ -0,0 +1,2 @@
+class Guid < ActiveRecord::Base
+end
View
4 activerecord/test/schema/schema.rb
@@ -403,6 +403,10 @@ def create_table(*args, &block)
create_table(t, :force => true) { }
end
+ create_table :guids, :force => true do |t|
+ t.column :key, :string
+ end
+
except 'SQLite' do
# fk_test_has_fk should be before fk_test_has_pk
create_table :fk_test_has_fk, :force => true do |t|

0 comments on commit 4cc594b

Please sign in to comment.