Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-partial inserts for models with composite identity primary keys #51859

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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