Skip to content

Be more flexible with the arity of ActiveRecord::StatementErrors' constructor #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v4.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ def initialize_type_map(map)
def translate_exception(exception, message)
case exception.message[/^\d+/].to_i
when ERR_DUPLICATE_KEY_VALUE
ActiveRecord::RecordNotUnique.new(message, exception)
args = [message]
args << exception if ActiveRecord::RecordNotUnique.instance_method(:initialize).arity == 2

ActiveRecord::RecordNotUnique.new(*args)
else
super
end
Expand Down
8 changes: 8 additions & 0 deletions test/crud_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def test_destroy
end
end

def test_record_not_unqiue
with_transaction do
assert_raises ActiveRecord::StatementInvalid do
User.create(id: 1, first_name: 'Jack', last_name: 'Duplicate')
end
end
end

private

def with_transaction(&_block)
Expand Down