Skip to content

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
@earshinov earshinov force-pushed the relationship-inheritance branch from f95b739 to 766d08a Compare April 12, 2024 12:21
@alejsdev alejsdev added the feature New feature or request label Jul 5, 2024
@earshinov earshinov force-pushed the relationship-inheritance branch 2 times, most recently from 7fd0c5f to 46c8d62 Compare September 2, 2025 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants