Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def __init__(__pydantic_self__, **data: Any) -> None:
for key in non_pydantic_keys:
if key in __pydantic_self__.__sqlmodel_relationships__:
setattr(__pydantic_self__, key, data[key])
__pydantic_self__._init_private_attributes()

def __setattr__(self, name: str, value: Any) -> None:
if name in {"_sa_instance_state"}:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_private_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Optional

from pydantic import PrivateAttr
from sqlmodel import Field, SQLModel


def test_private_attribute():
class Hero(SQLModel, table=True):
primary_key: Optional[int] = Field(
default=None,
primary_key=True,
)
_private_attribute: str = PrivateAttr(default="my_private_attribute")

hero = Hero()
assert hero._private_attribute == "my_private_attribute"
assert "primary_key" in hero.dict()
assert "_private_attribute" not in hero.dict()