Skip to content

Respect DB column defaults over attribute :foo, default: X overrides#358

Merged
OdenTakashi merged 2 commits into
drwl:mainfrom
kamipo:respect-db-defaults-over-attribute-overrides
Jul 15, 2026
Merged

Respect DB column defaults over attribute :foo, default: X overrides#358
OdenTakashi merged 2 commits into
drwl:mainfrom
kamipo:respect-db-defaults-over-attribute-overrides

Conversation

@kamipo

@kamipo kamipo commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Model#column_defaults reflects attribute overrides declared with attribute :foo, default: X, so annotations previously showed the overridden Ruby value instead of the actual DB default. Derive defaults from columns_hash and typecast via the schema-level cast type so annotations mirror db/schema.rb.

kamipo added 2 commits July 14, 2026 20:07
`Model#column_defaults` reflects attribute overrides declared with
`attribute :foo, default: X`, so annotations previously showed the
overridden Ruby value instead of the actual DB default. Derive defaults
from `columns_hash` and typecast via the schema-level cast type so
annotations mirror `db/schema.rb`.
Deserializing `column.default` directly via the schema cast type loses
model-level type decorations such as `TimeZoneConverter`, so datetime
defaults previously rendered as `Tue, 04 Jul 2023 12:34:56.000000000 UTC
+00:00` regressed to `2023-07-04 12:34:56 UTC` in annotations. Start
from `Model#column_defaults` and only substitute the schema-derived
value when it mismatches — a mismatch indicates an `attribute :foo,
default: X` override.

@OdenTakashi OdenTakashi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix!

I agree with this change — Schema Information should reflect the DB schema default, not Ruby-side attribute overrides.

@drwl
One thing to keep in mind for release: this change will update existing annotations for models that override DB defaults with attribute. For example:

# DB default: "hello world!"
attribute :message, :string, default: "overridden by the model"

Before:

#  message :string default("overridden by the model")

After:

#  message :string default("hello world!")

Columns without a database default are unaffected, and datetime defaults that are not overridden should keep their existing display format.

This might be a useful note for the release notes so users understand why their existing annotations might change after upgrading.

@OdenTakashi
OdenTakashi merged commit c893d14 into drwl:main Jul 15, 2026
61 of 62 checks passed
@kamipo
kamipo deleted the respect-db-defaults-over-attribute-overrides branch July 15, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants