Skip to content

Commit 10b36be

Browse files
authored
Merge pull request rails#49692 from mguan2020/branchi
Fix issue with wrong argument type when using prefetch_primary_key?
2 parents 23399b2 + e1a3add commit 10b36be

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ def explain(arel, binds = [], options = []) # :nodoc:
189189
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)
190190
sql, binds = to_sql_and_binds(arel, binds)
191191
value = exec_insert(sql, name, binds, pk, sequence_name, returning: returning)
192-
return id_value if id_value
193-
returning.nil? ? last_inserted_id(value) : returning_column_values(value)
192+
193+
return returning_column_values(value) unless returning.nil?
194+
195+
id_value || last_inserted_id(value)
194196
end
195197
alias create insert
196198

activerecord/test/cases/persistence_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require "models/cpk"
2525
require "models/chat_message"
2626
require "models/default"
27+
require "models/post_with_prefetched_pk"
2728

2829
class PersistenceTest < ActiveRecord::TestCase
2930
fixtures :topics, :companies, :developers, :accounts, :minimalistics, :authors, :author_addresses,
@@ -529,6 +530,11 @@ def test_create
529530
assert_equal("New Topic", topic_reloaded.title)
530531
end
531532

533+
def test_create_prefetched_pk
534+
post = PostWithPrefetchedPk.create!(title: "New Message", body: "New Body")
535+
assert_equal 123456, post.id
536+
end
537+
532538
def test_create_model_with_uuid_pk_populates_id
533539
message = ChatMessage.create(content: "New Message")
534540
assert_not_nil message.id
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class PostWithPrefetchedPk < ActiveRecord::Base
4+
self.table_name = "posts"
5+
6+
class << self
7+
def prefetch_primary_key?
8+
true
9+
end
10+
11+
def next_sequence_value
12+
123456
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)