Skip to content

Commit ae3c93d

Browse files
authored
Merge pull request rails#51859 from fatkodima/fix-partial_inserts-with-identity-cpk
Fix non-partial inserts for models with composite identity primary keys
2 parents db93271 + 0318c34 commit ae3c93d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

activerecord/lib/active_record/attribute_methods/dirty.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def attribute_names_for_partial_inserts
251251
changed_attribute_names_to_save
252252
else
253253
attribute_names.reject do |attr_name|
254-
if column_for_attribute(attr_name).default_function
254+
if column_for_attribute(attr_name).auto_populated?
255255
!attribute_changed?(attr_name)
256256
end
257257
end

activerecord/test/cases/dirty_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,20 @@ def check_around
972972
end
973973
end
974974

975+
if current_adapter?(:PostgreSQLAdapter) && supports_identity_columns?
976+
test "partial insert off with changed composite identity primary key attribute" do
977+
klass = Class.new(ActiveRecord::Base) do
978+
self.table_name = "cpk_postgresql_identity_table"
979+
end
980+
981+
with_partial_writes(klass, false) do
982+
record = klass.create!(another_id: 10)
983+
assert_equal 10, record.another_id
984+
assert_not_nil record.id
985+
end
986+
end
987+
end
988+
975989
test "attribute_changed? properly type casts enum values" do
976990
parrot = LiveParrot.create!(name: "Scipio", breed: :african)
977991

activerecord/test/schema/postgresql_specific_schema.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
5454
)
5555
SQL
56+
57+
drop_table "cpk_postgresql_identity_table", if_exists: true
58+
execute <<~SQL
59+
create table cpk_postgresql_identity_table (
60+
another_id INT NOT NULL,
61+
id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
62+
CONSTRAINT cpk_postgresql_identity_table_pkey PRIMARY KEY (another_id, id)
63+
)
64+
SQL
5665
end
5766

5867
create_table :postgresql_times, force: true do |t|

0 commit comments

Comments
 (0)