Skip to content

Commit

Permalink
a friendly little helper
Browse files Browse the repository at this point in the history
  • Loading branch information
simon committed Sep 28, 2024
1 parent 23e9797 commit 6f55a91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions reflex/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from reflex.base import Base
from reflex.config import get_config
from reflex.utils import console
from reflex.utils.compat import sqlmodel
from reflex.utils.compat import sqlmodel, sa_column_id_supporter


def get_engine(url: str | None = None) -> sqlalchemy.engine.Engine:
Expand Down Expand Up @@ -166,10 +166,10 @@ def __init_subclass__(cls):
non_default_primary_key_fields = [
field_name
for field_name, field in cls.__fields__.items()
if field_name != "id"
and getattr(field.field_info, "primary_key", None) is True
if field_name != "id" and sa_column_id_supporter(field)
]
if non_default_primary_key_fields:
print(cls.__name__, non_default_primary_key_fields)
cls.__fields__.pop("id", None)

super().__init_subclass__()
Expand Down
15 changes: 15 additions & 0 deletions reflex/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ def pydantic_v1_patch():

with pydantic_v1_patch():
import sqlmodel as sqlmodel


def sa_column_id_supporter(field) -> bool:
"""Adds support for sa_column so we don't add id with a primary key
is defined
AKA simons_litttle_helper
AKA masens_litttlerrr_helperr
"""
if getattr(field.field_info, "primary_key", None) is True:
return True
if getattr(field.field_info, "sa_column", None) is not None:
if getattr(field.field_info.sa_column, "primary_key", None) is True:
return True
return False

0 comments on commit 6f55a91

Please sign in to comment.