🐛 Move __tablename__ default from @declared_attr to metaclass#1821
Open
estarfoo wants to merge 1 commit intofastapi:mainfrom
Open
🐛 Move __tablename__ default from @declared_attr to metaclass#1821estarfoo wants to merge 1 commit intofastapi:mainfrom
__tablename__ default from @declared_attr to metaclass#1821estarfoo wants to merge 1 commit intofastapi:mainfrom
Conversation
The `SQLModel` base class declared `__tablename__` both as a `ClassVar` and as a `@declared_attr` method. Some type checkers (pyright) see the descriptor type from `@declared_attr`, so setting `__tablename__ = "my_table"` in a subclass is rejected as a type mismatch, even though it works at runtime. Replace the `@declared_attr` method with a default set in `SQLModelMetaclass.__new__` via `dict_used`, before class creation. Fixes fastapi#98. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Move the default
__tablename__from a@declared_attrmethod toSQLModelMetaclass.__new__, where it is set in the class dict before class creation, alongside the existing setup.This resolves the type-level contradiction between the
ClassVar[str | Callable]declaration and the@declared_attrdescriptor, letting__tablename__ = "my_table"work without# type: ignorefor pyright.Tests added for default name, explicit override, inheritance, and non-table models.
Fixes #98.
This is split out from #1820, dropping those changes which would be resolved by #1345 or #1806.