FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add support for specifying column type. by tasn · Pull Request #111 · fastapi/sqlmodel · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 15 additions & 1 deletion sqlmodel/main.py
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ipaddress
from sqlalchemy.sql.type_api import TypeEngine
import uuid
import weakref
from datetime import date, datetime, time, timedelta
Expand Down Expand Up @@ -72,6 +73,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
foreign_key = kwargs.pop("foreign_key", Undefined)
index = kwargs.pop("index", Undefined)
sa_column = kwargs.pop("sa_column", Undefined)
sa_column_type = kwargs.pop("sa_column_type", Undefined)
sa_column_args = kwargs.pop("sa_column_args", Undefined)
sa_column_kwargs = kwargs.pop("sa_column_kwargs", Undefined)
if sa_column is not Undefined:
Expand All @@ -91,6 +93,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
self.foreign_key = foreign_key
self.index = index
self.sa_column = sa_column
self.sa_column_type = sa_column_type
self.sa_column_args = sa_column_args
self.sa_column_kwargs = sa_column_kwargs

Expand Down Expand Up @@ -153,6 +156,7 @@ def Field(
nullable: Union[bool, UndefinedType] = Undefined,
index: Union[bool, UndefinedType] = Undefined,
sa_column: Union[Column, UndefinedType] = Undefined,
sa_column_type: Union[TypeEngine, UndefinedType] = Undefined,
sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined,
sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined,
schema_extra: Optional[Dict[str, Any]] = None,
Expand Down Expand Up @@ -183,6 +187,7 @@ def Field(
nullable=nullable,
index=index,
sa_column=sa_column,
sa_column_type=sa_column_type,
sa_column_args=sa_column_args,
sa_column_kwargs=sa_column_kwargs,
**current_schema_extra,
Expand Down Expand Up @@ -370,6 +375,11 @@ def __init__(


def get_sqlachemy_type(field: ModelField) -> Any:
if hasattr(field.type_, "sa_column_type"):
sa_column_type = getattr(field.type_, "sa_column_type")
if callable(sa_column_type):
return sa_column_type()
return sa_column_type
if issubclass(field.type_, str):
if field.field_info.max_length:
return AutoString(length=field.field_info.max_length)
Expand Down Expand Up @@ -412,7 +422,11 @@ def get_column_from_field(field: ModelField) -> Column:
sa_column = getattr(field.field_info, "sa_column", Undefined)
if isinstance(sa_column, Column):
return sa_column
sa_type = get_sqlachemy_type(field)
sa_column_type = getattr(field.field_info, "sa_column_type", Undefined)
if sa_column_type is not Undefined:
sa_type = sa_column_type
else:
sa_type = get_sqlachemy_type(field)
primary_key = getattr(field.field_info, "primary_key", False)
nullable = not field.required
index = getattr(field.field_info, "index", Undefined)
Expand Down

Back | FazBrowse Home | New Git URL