1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Pass pk: false to connection.insert explicitly if do not have a primary key

Because causing an extra query by `sql_for_insert` for guessing a
primary key.
https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb#L121-L125
This commit is contained in:
Ryuta Kamizono 2016-07-01 16:10:27 +09:00
parent 49a881e0db
commit 5d3a0b14c0
2 changed files with 9 additions and 1 deletions

View file

@ -65,7 +65,7 @@ module ActiveRecord
@klass.connection.insert(
im,
'SQL',
primary_key,
primary_key || false,
primary_key_value,
nil,
binds)

View file

@ -174,6 +174,14 @@ class PrimaryKeysTest < ActiveRecord::TestCase
assert_equal '2', dashboard.id
end
def test_create_without_primary_key_no_extra_query
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'dashboards'
end
klass.create! # warmup schema cache
assert_queries(3, ignore_none: true) { klass.create! }
end
if current_adapter?(:PostgreSQLAdapter)
def test_serial_with_quoted_sequence_name
column = MixedCaseMonkey.columns_hash[MixedCaseMonkey.primary_key]