Repository navigation
Conversation
msmakb
force-pushed
the
fix/frozen-field-assignment
branch
from
November 21, 2026 10:15
ee95e3b to
439ddaa
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
Discussion: #2116
Description
Fixes the assignment bug reported in discussion #2116. Previously, assigning to a frozen field on a database-loaded table model raised ValidationError after SQLAlchemy had already recorded the change. Catching the error and committing the session could persist the rejected value.
SQLModel.setattr now checks whether the field or model is frozen before calling set_attribute(). A rejected assignment raises Pydantic’s frozen_field or frozen_instance error before SQLAlchemy fires attribute listeners or records history. Accepted assignments keep their existing event order.
Tests
Added five regression cases covering loaded and expired fields, frozen primary keys, and foreign-key synchronization. They verify that a rejected write leaves the original value intact while valid pending edits still commit.
All five cases failed with the original setter and passed with the fix. All 147 synchronous runtime tests passed.
Performance
Local benchmarks on Python 3.15.2 showed an additional 0.30 µs per writable mapped-field assignment. Rejecting a frozen write was 46% faster because the check runs before SQLAlchemy processes the assignment.
Values are median times per assignment across 12 alternating rounds of 15,000 assignments per case. The benchmark measured the setter in isolation.
AI Disclaimer
Too many prompts and many solutions proposed, but I wasn't convened of them all except this solution to have minimal change with much less affect, also considering the performance side as this will be run for every assignment in general.
Model used: GPT-Astora-6.2-Max
Checklist