Skip to content

Commit 1580ecc

Browse files
committed
Enhance PydanticJSONB to use JSON variant for better compatibility across databases
1 parent 483cea7 commit 1580ecc

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

sqlmodel/sql/sqltypes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
from pydantic import BaseModel
1515
from pydantic_core import to_jsonable_python
16-
from sqlalchemy import types
16+
from sqlalchemy import JSON, types
1717
from sqlalchemy.dialects.postgresql import JSONB # for Postgres JSONB
1818
from sqlalchemy.engine.interfaces import Dialect
1919

2020
BaseModelType = TypeVar("BaseModelType", bound=BaseModel)
2121

2222
# Define a type alias for JSON-serializable values
2323
JSONValue = Union[Dict[str, Any], List[Any], str, int, float, bool, None]
24+
JSON_VARIANT = JSON().with_variant(JSONB, "postgresql")
2425

2526

2627
class AutoString(types.TypeDecorator): # type: ignore
@@ -38,7 +39,7 @@ def load_dialect_impl(self, dialect: Dialect) -> types.TypeEngine[Any]:
3839
class PydanticJSONB(types.TypeDecorator): # type: ignore
3940
"""Custom type to automatically handle Pydantic model serialization."""
4041

41-
impl = JSONB # use JSONB type in Postgres (fallback to JSON for others)
42+
impl = JSON_VARIANT
4243
cache_ok = True # allow SQLAlchemy to cache results
4344

4445
def __init__(

0 commit comments

Comments
 (0)