From 0318c34cb3769bccf949c2a0f208b3e40072a949 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sat, 18 May 2024 12:08:33 +0300 Subject: [PATCH] Fix non-partial inserts for models with composite identity primary keys --- .../lib/active_record/attribute_methods/dirty.rb | 2 +- activerecord/test/cases/dirty_test.rb | 14 ++++++++++++++ .../test/schema/postgresql_specific_schema.rb | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 40b58606eb1..d393f1d41fb 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -251,7 +251,7 @@ module ActiveRecord changed_attribute_names_to_save else attribute_names.reject do |attr_name| - if column_for_attribute(attr_name).default_function + if column_for_attribute(attr_name).auto_populated? !attribute_changed?(attr_name) end end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index f7c4a9e423f..658309fe06a 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -972,6 +972,20 @@ class DirtyTest < ActiveRecord::TestCase end end + if current_adapter?(:PostgreSQLAdapter) && supports_identity_columns? + test "partial insert off with changed composite identity primary key attribute" do + klass = Class.new(ActiveRecord::Base) do + self.table_name = "cpk_postgresql_identity_table" + end + + with_partial_writes(klass, false) do + record = klass.create!(another_id: 10) + assert_equal 10, record.another_id + assert_not_nil record.id + end + end + end + test "attribute_changed? properly type casts enum values" do parrot = LiveParrot.create!(name: "Scipio", breed: :african) diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index 13d64b061d4..e8be384b183 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -53,6 +53,15 @@ ActiveRecord::Schema.define do id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY ) SQL + + drop_table "cpk_postgresql_identity_table", if_exists: true + execute <<~SQL + create table cpk_postgresql_identity_table ( + another_id INT NOT NULL, + id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, + CONSTRAINT cpk_postgresql_identity_table_pkey PRIMARY KEY (another_id, id) + ) + SQL end create_table :postgresql_times, force: true do |t|