Skip to content

Commit

Permalink
Merge pull request #51859 from fatkodima/fix-partial_inserts-with-ide…
Browse files Browse the repository at this point in the history
…ntity-cpk

Fix non-partial inserts for models with composite identity primary keys
  • Loading branch information
yahonda committed May 27, 2024
2 parents db93271 + 0318c34 commit ae3c93d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def attribute_names_for_partial_inserts
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
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,20 @@ def check_around
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)

Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
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|
Expand Down

0 comments on commit ae3c93d

Please sign in to comment.