Add interface to override get_column_from_field
| get_sqlalchemy_type
function behavior
#503
Open
8 tasks done
Labels
feature
New feature or request
First Check
Commit to Help
Example Code
Description
Problem:
SQLModel
does not provide expected column type, and it is (in some cases) impossible because requirements ofSQLModel
users are not always the same (e.g. DBMS dialects, strictness of constraints, choice of CHAR vs VARCHAR vs TEXT vs JSON, TIMESTAMP vs DATETIME)Wanted Solution
Allow user to use customized
get_column_from_field
|get_sqlalchemy_type
function to fit with their own requirements.Add parameter to model config like
sa_column_builder: Callable[[ModelField], Column] = get_column_from_field
.Function
get_column_from_field
would be better split by the following concerns, to be used as a part of customizedsa_column_builder
implementation:get_sqlalchemy_type
)Possible effects on other issues/PRs:
sqlmodel
users:p.s-1
Conversion rule between Field/column value may become necessary, mainly to serialize field value to column value.
(e.g. Classes inheriting BaseModel cannot be stored directly into
sqlalchemy.JSON
because it is not JSON or dict. We avoid this by addingjson_serializer
tocreate_engine
. Deserialize part has no problem because JSONstr
->BaseModel
will be done by pydantic validation for now (pydantic v1))p.s-2
IMO using
sqlmodel.sql.sqltypes.AutoString()
in alembic revision file is not good from the sight of future migration constancy, and this is one of the reason I overriddenget_sqlalchemy_type
function.Wanted Code
Alternatives
sa_column
and call it insqlmodel.Field
declarationConstrainedStr
, ...)Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.8
Python Version
3.10.7
Additional Context
No response
The text was updated successfully, but these errors were encountered: