Skip to content

Commit

Permalink
Fix issue with wrong argument type when using prefetch_primary_key?
Browse files Browse the repository at this point in the history
  • Loading branch information
mguan2020 authored and rafaelfranca committed Nov 10, 2023
1 parent 00dfa10 commit e1a3add
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Expand Up @@ -189,8 +189,10 @@ def explain(arel, binds = [], options = []) # :nodoc:
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)
sql, binds = to_sql_and_binds(arel, binds)
value = exec_insert(sql, name, binds, pk, sequence_name, returning: returning)
return id_value if id_value
returning.nil? ? last_inserted_id(value) : returning_column_values(value)

return returning_column_values(value) unless returning.nil?

id_value || last_inserted_id(value)
end
alias create insert

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/persistence_test.rb
Expand Up @@ -24,6 +24,7 @@
require "models/cpk"
require "models/chat_message"
require "models/default"
require "models/post_with_prefetched_pk"

class PersistenceTest < ActiveRecord::TestCase
fixtures :topics, :companies, :developers, :accounts, :minimalistics, :authors, :author_addresses,
Expand Down Expand Up @@ -504,6 +505,11 @@ def test_create
assert_equal("New Topic", topic_reloaded.title)
end

def test_create_prefetched_pk
post = PostWithPrefetchedPk.create!(title: "New Message", body: "New Body")
assert_equal 123456, post.id
end

def test_create_model_with_uuid_pk_populates_id
message = ChatMessage.create(content: "New Message")
assert_not_nil message.id
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/models/post_with_prefetched_pk.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class PostWithPrefetchedPk < ActiveRecord::Base
self.table_name = "posts"

class << self
def prefetch_primary_key?
true
end

def next_sequence_value
123456
end
end
end

0 comments on commit e1a3add

Please sign in to comment.