diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-01-05 14:19:01 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-01-05 14:19:01 -0700 |
commit | 08d6eb2370bf44d451503bc58b58c0192d820341 (patch) | |
tree | b9b0ab6e01d2a39a0838a6543c332d2b705d4164 | |
parent | 1c8192669fd6880041f87ba8ee2aae52b1f7edd8 (diff) | |
parent | f634c1fcf4796f633685f6801e260e0e792e547e (diff) | |
download | rails-08d6eb2370bf44d451503bc58b58c0192d820341.tar.gz rails-08d6eb2370bf44d451503bc58b58c0192d820341.tar.bz2 rails-08d6eb2370bf44d451503bc58b58c0192d820341.zip |
Merge pull request #18349 from jdelStrother/primarykeyless
Fix rollback of primarykey-less tables
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 22 |
2 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 1c48196762..9cef50029b 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -378,7 +378,7 @@ module ActiveRecord thaw unless restore_state[:frozen?] @new_record = restore_state[:new_record] @destroyed = restore_state[:destroyed] - write_attribute(self.class.primary_key, restore_state[:id]) + write_attribute(self.class.primary_key, restore_state[:id]) if self.class.primary_key end end end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 27a7c9212b..466ab45502 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -650,6 +650,28 @@ class TransactionTest < ActiveRecord::TestCase assert transaction.state.committed? end + def test_transaction_rollback_with_primarykeyless_tables + connection = ActiveRecord::Base.connection + connection.create_table(:transaction_without_primary_keys, force: true, id: false) do |t| + t.integer :thing_id + end + + klass = Class.new(ActiveRecord::Base) do + self.table_name = 'transaction_without_primary_keys' + after_commit { } # necessary to trigger the has_transactional_callbacks branch + end + + assert_no_difference(-> { klass.count }) do + ActiveRecord::Base.transaction do + klass.create! + raise ActiveRecord::Rollback + end + end + + ensure + connection.execute("DROP TABLE IF EXISTS transaction_without_primary_keys") + end + private %w(validation save destroy).each do |filter| |