Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

earshinov
Copy link

@earshinov earshinov commented Apr 8, 2024

Please see the test I added:

class CreatedUpdatedMixin(SQLModel):
    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)
        )
    )

class Asset(CreatedUpdatedMixin, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)

Given how the test model is defined and without the proposed fix, the test fails with:

        with Session(engine) as session:
            asset = session.exec(select(Asset)).one()
>           assert asset.created_by.name == "John"
E           AttributeError: 'Asset' object has no attribute 'created_by'

That is, a Relationship defined in the base model is not recognized as a relationship in the child.

@earshinov earshinov force-pushed the relationship-inheritance branch 6 times, most recently from 6d05a4b to 8f30325 Compare April 8, 2024 17:50
@earshinov earshinov changed the title WIP: ✨Properly support inheritance of Relationship attributes ✨Properly support inheritance of Relationship attributes Apr 8, 2024
@earshinov earshinov force-pushed the relationship-inheritance branch 7 times, most recently from 2200fc5 to f95b739 Compare April 12, 2024 12:12
@@ -68,7 +68,7 @@ jobs:
run: pip install --upgrade "pydantic>=1.10.0,<2.0.0"
- name: Install Pydantic v2
if: matrix.pydantic-version == 'pydantic-v2'
run: pip install --upgrade "pydantic>=2.0.2,<3.0.0"
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants