Skip to content

✨ Make field's description interpret as column's comment #1335

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ def get_column_from_field(field: Any) -> Column: # type: ignore
"index": index,
"unique": unique,
}
description = getattr(field_info, "description", Undefined)
if description is not Undefined:
kwargs["comment"] = description
sa_default = Undefined
if field_info.default_factory:
sa_default = field_info.default_factory
Expand Down
23 changes: 23 additions & 0 deletions tests/test_field_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pytest import LogCaptureFixture
from sqlmodel import Field, SQLModel, create_engine


def test_sa_column_description(clear_sqlmodel: None, caplog: LogCaptureFixture) -> None:
class Team(SQLModel, table=True):
id: int = Field(primary_key=True, description="an id")
name: str = Field(description="a name")
age: int = Field()

assert Team.model_fields["id"].description == "an id"
assert Team.model_fields["name"].description == "a name"
assert Team.model_fields["age"].description is None

engine = create_engine("sqlite://", echo=True) # TODO: this should go to Postgres
SQLModel.metadata.create_all(engine)
msgs = []
for msg in caplog.messages:
if "COMMENT ON COLUMN" in msg:
msgs.append(msg)
assert len(msgs) == 2
assert "COMMENT ON COLUMN team.id IS 'an id'" in msgs[0]
assert "COMMENT ON COLUMN team.name IS 'a name'" in msgs[1]
Loading