-
-
Notifications
You must be signed in to change notification settings - Fork 666
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
✨Properly support inheritance of Relationship attributes #886
Open
earshinov
wants to merge
4
commits into
fastapi:main
Choose a base branch
from
earshinov:relationship-inheritance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e9f6a4c
✨Properly support inheritance of Relationship attributes
earshinov 143421e
For sake of demonstration, add created_at and updated_at fields to Cr…
earshinov 2f20565
✏️Fix `model_validate` in presence of inherited Relationship fields, …
earshinov 766d08a
‼️Temporarily avoid Pydantic 2.7.0 in the CI pipeline
earshinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import datetime | ||
from typing import Optional | ||
|
||
import pydantic | ||
from sqlalchemy import DateTime, func | ||
from sqlalchemy.orm import declared_attr, relationship | ||
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select | ||
from sqlmodel._compat import IS_PYDANTIC_V2 | ||
|
||
|
||
def test_inherit_relationship(clear_sqlmodel) -> None: | ||
def now(): | ||
return datetime.datetime.now(tz=datetime.timezone.utc) | ||
|
||
class User(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
|
||
class CreatedUpdatedMixin(SQLModel): | ||
# Fields in reusable base models must be defined using `sa_type` and `sa_column_kwargs` instead of `sa_column` | ||
# https://github.com/tiangolo/sqlmodel/discussions/743 | ||
# | ||
# created_at: datetime.datetime = Field(default_factory=now, sa_column=DateTime(default=now)) | ||
created_at: datetime.datetime = Field( | ||
default_factory=now, sa_type=DateTime, sa_column_kwargs={"default": now} | ||
) | ||
|
||
# With Pydantic V2, it is also possible to define `created_by` like this: | ||
# | ||
# ```python | ||
# @declared_attr | ||
# def _created_by(cls): | ||
# return relationship(User, foreign_keys=cls.created_by_id) | ||
# | ||
# created_by: Optional[User] = Relationship(sa_relationship=_created_by)) | ||
# ``` | ||
# | ||
# The difference from Pydantic V1 is that Pydantic V2 plucks attributes with names starting with '_' (but not '__') | ||
# from class attributes and stores them separately as instances of `pydantic.ModelPrivateAttr` somewhere in depths of | ||
# Pydantic internals. Under Pydantic V1 this doesn't happen, so SQLAlchemy ends up having two class attributes | ||
# (`_created_by` and `created_by`) corresponding to one database attribute, causing a conflict and unreliable behavior. | ||
# The approach with a lambda always works because it doesn't produce the second class attribute and thus eliminates | ||
# the possibility of a conflict entirely. | ||
# | ||
created_by_id: Optional[int] = Field(default=None, foreign_key="user.id") | ||
created_by: Optional[User] = Relationship( | ||
sa_relationship=declared_attr( | ||
lambda cls: relationship(User, foreign_keys=cls.created_by_id) | ||
) | ||
) | ||
|
||
updated_at: datetime.datetime = Field( | ||
default_factory=now, sa_type=DateTime, sa_column_kwargs={"default": now} | ||
) | ||
updated_by_id: Optional[int] = Field(default=None, foreign_key="user.id") | ||
updated_by: Optional[User] = Relationship( | ||
sa_relationship=declared_attr( | ||
lambda cls: relationship(User, foreign_keys=cls.updated_by_id) | ||
) | ||
) | ||
|
||
class Asset(CreatedUpdatedMixin, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
|
||
# Demonstrate that the mixin can be applied to more than 1 model | ||
class Document(CreatedUpdatedMixin, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
john = User(name="John") | ||
jane = User(name="Jane") | ||
asset = Asset(name="Test", created_by=john, updated_by=jane) | ||
doc = Document(name="Resume", created_by=jane, updated_by=john) | ||
|
||
with Session(engine) as session: | ||
session.add(asset) | ||
session.add(doc) | ||
session.commit() | ||
|
||
with Session(engine) as session: | ||
assert session.scalar(select(func.count()).select_from(User)) == 2 | ||
|
||
asset = session.exec(select(Asset)).one() | ||
assert asset.created_by.name == "John" | ||
assert asset.updated_by.name == "Jane" | ||
|
||
doc = session.exec(select(Document)).one() | ||
assert doc.created_by.name == "Jane" | ||
assert doc.updated_by.name == "John" | ||
|
||
|
||
def test_inherit_relationship_model_validate(clear_sqlmodel) -> None: | ||
class User(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
|
||
class Mixin(SQLModel): | ||
owner_id: Optional[int] = Field(default=None, foreign_key="user.id") | ||
owner: Optional[User] = Relationship( | ||
sa_relationship=declared_attr( | ||
lambda cls: relationship(User, foreign_keys=cls.owner_id) | ||
) | ||
) | ||
|
||
class Asset(Mixin, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
|
||
class AssetCreate(pydantic.BaseModel): | ||
pass | ||
|
||
asset_create = AssetCreate() | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
user = User() | ||
|
||
# Owner must be optional | ||
asset = Asset.model_validate(asset_create) | ||
with Session(engine) as session: | ||
session.add(asset) | ||
session.commit() | ||
session.refresh(asset) | ||
assert asset.id is not None | ||
assert asset.owner_id is None | ||
assert asset.owner is None | ||
|
||
# When set, owner must be saved | ||
# | ||
# Under Pydantic V2, relationship fields set it `model_validate` are not saved, | ||
# with or without inheritance. Consider it a known issue. | ||
# | ||
if IS_PYDANTIC_V2: | ||
asset = Asset.model_validate(asset_create, update={"owner": user}) | ||
with Session(engine) as session: | ||
session.add(asset) | ||
session.commit() | ||
session.refresh(asset) | ||
session.refresh(user) | ||
assert asset.id is not None | ||
assert user.id is not None | ||
assert asset.owner_id == user.id | ||
assert asset.owner.id == user.id |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be rolled back before merging