Skip to content

Commit a4f8336

Browse files
committed
Make field's description interpret as column's comment
1 parent e7744e5 commit a4f8336

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

sqlmodel/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,9 @@ def get_column_from_field(field: Any) -> Column: # type: ignore
744744
"index": index,
745745
"unique": unique,
746746
}
747+
description = getattr(field_info, "description", Undefined)
748+
if description is not Undefined:
749+
kwargs["comment"] = description
747750
sa_default = Undefined
748751
if field_info.default_factory:
749752
sa_default = field_info.default_factory

tests/test_field_description.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pytest import LogCaptureFixture
2+
from sqlmodel import Field, SQLModel, create_engine
3+
4+
5+
def test_sa_column_description(clear_sqlmodel: None, caplog: LogCaptureFixture) -> None:
6+
class Team(SQLModel, table=True):
7+
id: int = Field(primary_key=True, description="an id")
8+
name: str = Field(description="a name")
9+
age: int = Field()
10+
11+
assert Team.model_fields["id"].description == "an id"
12+
assert Team.model_fields["name"].description == "a name"
13+
assert Team.model_fields["age"].description is None
14+
15+
engine = create_engine("sqlite://", echo=True) # TODO: this should go to Postgres
16+
SQLModel.metadata.create_all(engine)
17+
msgs = []
18+
for msg in caplog.messages:
19+
if "COMMENT ON COLUMN" in msg:
20+
msgs.append(msg)
21+
assert len(msgs) == 2
22+
assert "COMMENT ON COLUMN team.id IS 'an id'" in msgs[0]
23+
assert "COMMENT ON COLUMN team.name IS 'a name'" in msgs[1]

0 commit comments

Comments
 (0)