Skip to content

Commit

Permalink
Fix uniqueness validation to correctly work with expression indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Oct 4, 2023
1 parent 536d168 commit ef1968b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/validations/uniqueness.rb
Expand Up @@ -87,7 +87,7 @@ def covered_by_unique_index?(klass, record, attribute, scope)
klass.connection.schema_cache.indexes(klass.table_name).any? do |index|
index.unique &&
index.where.nil? &&
(index.columns - attributes).empty?
(Array(index.columns) - attributes).empty?
end
end

Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/validations/uniqueness_validation_test.rb
Expand Up @@ -812,6 +812,20 @@ def test_index_of_columns_list_and_extra_columns
t.valid?
end
end

if current_adapter?(:PostgreSQLAdapter)
def test_expression_index
Topic.validates_uniqueness_of(:title)
@connection.add_index(:topics, "LOWER(title)", unique: true, name: :topics_index)

t = Topic.create!(title: "abc", author_name: "John")
t.content = "hello world"

assert_queries(1) do
t.valid?
end
end
end
end

class UniquenessWithCompositeKey < ActiveRecord::TestCase
Expand Down

0 comments on commit ef1968b

Please sign in to comment.