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

馃悰 Fix setting nullable property of Fields that don't accept `None` by van51 路 Pull Request #79 路 fastapi/sqlmodel 路 GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) 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
13 changes: 12 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
Expand Up @@ -25,6 +25,7 @@

from pydantic import BaseConfig, BaseModel
from pydantic.errors import ConfigError, DictError
from pydantic.fields import SHAPE_SINGLETON
from pydantic.fields import FieldInfo as PydanticFieldInfo
from pydantic.fields import ModelField, Undefined, UndefinedType
from pydantic.main import ModelMetaclass, validate_model
Expand Down Expand Up @@ -424,14 +425,14 @@ def get_column_from_field(field: ModelField) -> Column: # type: ignore
return sa_column
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)
if index is Undefined:
index = False
if hasattr(field.field_info, "nullable"):
field_nullable = getattr(field.field_info, "nullable")
if field_nullable != Undefined:
nullable = field_nullable
nullable = not primary_key and _is_field_nullable(field)
args = []
foreign_key = getattr(field.field_info, "foreign_key", None)
if foreign_key:
Expand Down Expand Up @@ -646,3 +647,13 @@ def _calculate_keys(
@declared_attr # type: ignore
def __tablename__(cls) -> str:
return cls.__name__.lower()


def _is_field_nullable(field: ModelField) -> bool:
if not field.required:
# Taken from [Pydantic](https://github.com/samuelcolvin/pydantic/blob/v1.8.2/pydantic/fields.py#L946-L947)
is_optional = field.allow_none and (
field.shape != SHAPE_SINGLETON or not field.sub_fields
)
return is_optional and field.default is None and field.default_factory is None
return False
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
Expand Up @@ -9,7 +9,7 @@ def test_create_db_and_table(cov_tmp_path: Path):
assert "BEGIN" in result.stdout
assert 'PRAGMA main.table_info("hero")' in result.stdout
assert "CREATE TABLE hero (" in result.stdout
assert "id INTEGER," in result.stdout
assert "id INTEGER NOT NULL," in result.stdout
assert "name VARCHAR NOT NULL," in result.stdout
assert "secret_name VARCHAR NOT NULL," in result.stdout
assert "age INTEGER," in result.stdout
Expand Down

Back | FazBrowse Home | New Git URL