Skip to content

Conversation

@miry
Copy link
Contributor

@miry miry commented Dec 27, 2025

Problem

I found that if it is only require to update a single column in DB, all other fields also send to DB. Example of Logs for changing only field public_profile:

Executing query: UPDATE "users" SET "created_at"=$1, "updated_at"=$2, "email"=$3, "name"=$4, "code"=$5, "nickname"=$6, "public_profile"=$7, "referral_code"=$8, "referrer_id"=$9, "official_tag_en"=$10, "official_tag_uk"=$11, "total_filament_weight_printed"=$12, "total_filament_weight_printed_last_30"=$13 WHERE "id"=$14 -- prefix: "  ▸ "

Solution

Introduce new methods update_columns and update_columns! that allow updating specific database columns without running validations or callbacks. These methods provide a performance optimization for cases where only a subset of fields needs to be updated.

Key features:

  • Updates only specified columns, leaving others unchanged
  • Bypasses model validations and lifecycle callbacks
  • Updates both in-memory values and database records
  • update_columns! raises an error for unsaved records

Usage:
user.update_columns(username: "new_name") user.update_columns!(email: "new@example.com", last_login: Time.utc)

It generates logs:

Executing query: UPDATE "users" SET "public_profile"=$1 WHERE "id"=$2

@miry miry changed the title Add update_columns methods to DB::Model for efficient partial updates Add update_columns methods to DB::Model for partial updates Dec 27, 2025
@miry miry force-pushed the db-model-update-columns branch from 64a3290 to 9c1c34e Compare December 27, 2025 20:13
Introduce new methods `update_columns` and `update_columns!` that allow
updating specific database columns without running validations or
callbacks. These methods provide a performance optimization for cases
where only a subset of fields needs to be updated.

Key features:
- Updates only specified columns, leaving others unchanged
- Bypasses model validations and lifecycle callbacks
- Updates both in-memory values and database records
- `update_columns!` raises an error for unsaved records

Usage:
  user.update_columns(username: "new_name")
  user.update_columns!(email: "new@example.com", last_login: Time.utc)
@miry miry force-pushed the db-model-update-columns branch from 9c1c34e to cb6cfd2 Compare December 27, 2025 20:15
Copy link
Member

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one question, but the changes look good otherwise!

end

# :ditto:
def update_columns(values : Hash | NamedTuple) : Bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this method return false early if called on a new record?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in e3ca6a9

@miry miry force-pushed the db-model-update-columns branch from ccc91d3 to e3ca6a9 Compare January 15, 2026 17:24
Copy link
Member

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

@ellmetha ellmetha merged commit 98a3be0 into martenframework:main Jan 16, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants