-
-
Notifications
You must be signed in to change notification settings - Fork 748
✨ Support sqlalchemy MappedColumn
#1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
These tests share the same structure and field names, so they conflict if not cleaned up.
This PR is failing one test because it does not have a label assigned. Do I need to open a separate issue to point to the PR? |
isinstance directly supports using a tuple of allowed types.
Hello, While trying to fix a problem probably similar to yours (I want to make a column deferrable but it can only be made on at the end of According to tests, this is fully compatible. Also it's slightly more readable: not_loaded: str = Field(sa_column_kwargs={"deferred": True})
# vs
not_loaded: str = Field(sa_type=mapped_column(String, deferred=True)) Maybe there's an underlying reason why this cannot be acceptable but I think it's worth sharing (and also will help me follow this pull request) |
MappedColumn
When this PR will be merged? |
@spazm Thanks for this PR. This saved a day for me. def test_sa_column_no_foreign_key() -> None:
with pytest.raises(RuntimeError):
class Team(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
team_id: Optional[int] = Field(
default=None,
foreign_key="team.id",
sa_column=mapped_column(Integer, primary_key=True),
)
def test_sa_column_foreign_key_in_mapped_column_int() -> None:
class Team(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
team_id: Optional[int] = Field(
default=None,
sa_column=mapped_column(Integer, ForeignKey("team.id")),
)
def test_sa_column_foreign_key_in_mapped_column_custom_type() -> None:
from sqlmodel import String, TypeDecorator
class CustomType:
def __init__(self, value: str):
self.value = value
@classmethod
def from_str(cls, value: str) -> "CustomType":
return cls(value)
def __str__(self) -> str:
return self.value
class CustomTypeDecorator(TypeDecorator):
impl = String() # CustomType
cache_ok = True
def process_bind_param(self, value: CustomType, dialect):
if value is not None:
return str(value)
return None
def process_result_value(self, value, dialect):
if value is not None:
return CustomType.from_str(value)
return None
@property
def python_type(self):
return CustomType
class Team(SQLModel, table=True):
id: Optional[CustomType] = Field(
sa_type=CustomTypeDecorator, default=None, primary_key=True
)
name: str
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
team_id: Optional[CustomType] = Field(
default=None,
sa_column=mapped_column(CustomTypeDecorator, ForeignKey("team.id")),
)
def test_sa_column_no_unique() -> None:
... |
Reopening PR #896.
This PR adds a test with sqlmodel.orm.mapped_column working for sa_column. This test fails on current main.