diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-01 21:12:49 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-01 21:12:49 -0300 |
commit | 71acc3bd0100ba2ba32b8fe529dc3ea29b629126 (patch) | |
tree | 5132309391dbf4c1757c6d1c7f933ab6056cd714 | |
parent | 173bf3506d99c0767a41d30fbe4d306201369194 (diff) | |
parent | 5d3a0b14c0439beab8e3af946dec88fddc07f896 (diff) | |
download | rails-71acc3bd0100ba2ba32b8fe529dc3ea29b629126.tar.gz rails-71acc3bd0100ba2ba32b8fe529dc3ea29b629126.tar.bz2 rails-71acc3bd0100ba2ba32b8fe529dc3ea29b629126.zip |
Merge pull request #25620 from kamipo/create_without_primary_key
Pass `pk: false` to `connection.insert` explicitly if do not have a primary key
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/primary_keys_test.rb | 8 |
2 files changed, 9 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 7a1552856b..93baa882ad 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -65,7 +65,7 @@ module ActiveRecord @klass.connection.insert( im, 'SQL', - primary_key, + primary_key || false, primary_key_value, nil, binds) diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 6c8532cf00..4267ad4a24 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -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] |