diff options
author | Jonathan del Strother <jon.delStrother@audioboo.fm> | 2015-01-05 19:42:39 +0000 |
---|---|---|
committer | Jonathan del Strother <jon.delStrother@audioboo.fm> | 2015-01-05 20:47:41 +0000 |
commit | f634c1fcf4796f633685f6801e260e0e792e547e (patch) | |
tree | 67a065419c9db61380dcf87ba782cadd09f5bb78 /activerecord | |
parent | c7a1fa364012c70e76d19b36881c9bde016091b8 (diff) | |
download | rails-f634c1fcf4796f633685f6801e260e0e792e547e.tar.gz rails-f634c1fcf4796f633685f6801e260e0e792e547e.tar.bz2 rails-f634c1fcf4796f633685f6801e260e0e792e547e.zip |
Fix rollback of primarykey-less tables
If you have a table without a primary key, and an `after_commit` callback on that table (ie `has_transactional_callbacks?` returns true), then trying to rollback a transaction involving that record would result in “ActiveModel::MissingAttributeError: can't write unknown attribute ``”
Diffstat (limited to 'activerecord')
-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 31ca90fb58..97648ec898 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -387,7 +387,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 e0aecb5996..a89c24f57e 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -653,6 +653,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| |