Skip to content
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

Choose a reason for hiding this comment

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

Suggested change
kwargs["comment"] = description
kwargs["postgresql_comment"] = description

It might be worth trying with this to make the tests work to take advantage of dialect specific arguments. Then the tests should pass when using SQLite (after updating them as they currently only check for the comment kwarg).

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