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

Relationship type annotations disappear after class definition is evaluated #530

Open
8 tasks done
phdowling opened this issue Jan 15, 2023 · 1 comment
Open
8 tasks done
Labels
question Further information is requested

Comments

@phdowling
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlmodel import Relationship, SQLModel, Field
from pydantic.main import BaseModel

class SpecialFieldValidator(BaseModel):
    id: int = Field(primary_key=True, index=True)

    def __init_subclass__(cls) -> None:
        # Try to figure out what type the special_field relation has
        print("special_field" in cls.__annotations__)  # False
        print("special_field" in cls.__fields__)  # False
        print("special_field" in cls.__sqlmodel_relationships__)  # True, but no type info via this dict, just a plain RelationshipInfo() instance:
        print(dict(cls.__sqlmodel_relationships__["special_field"].__dict__))  # {'back_populates': None, 'link_model': None, 'sa_relationship': None, 'sa_relationship_args': None, 'sa_relationship_kwargs': None}

        return super().__init_subclass__()

class MyModelA(SQLModel, SpecialFieldValidator, table=True):
    id: int = Field(primary_key=True)
    special_field_id: int = Field(foreign_key="mymodelb.id")
    special_field: "MyModelB" = Relationship()

class MyModelB(SQLModel, SpecialFieldValidator, table=True):
    id: int = Field(primary_key=True)
    special_field_id: int = Field(foreign_key="mymodela.id")
    special_field: MyModelA = Relationship()

Description

I'm trying to write a class (SpecialFieldValidator) that is supposed to check whether a specific field, which is always a Relationship, was annotated with a particular type.

However, the actual type annotation seems to be getting erased - looking in fields, annotations yields nothing, and the data in sqlmodel_relationships does not feature a type.

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.8

Python Version

3.11.0

Additional Context

SQLAlchemy version is 1.4.41
I found this issue: #255 that seems related, saying SQLAlchemy 1.4.36 breaks relationship, but SQLModels dependencies were officially updated to >=1.4.41 more recently than that so I figured this is a new issue.

@phdowling phdowling added the question Further information is requested label Jan 15, 2023
@jarey
Copy link

jarey commented Dec 2, 2024

I'm also facing this issue. From what I could see, one potential solution could be the following (if support for relationship information as part of the pydantic model is not included as a feature in SQLModel fure releases, so consider this a workaround):

  • Override the __new__ dunder method ( as __init__ is not used by SQLModel).
  • Populate the pydantic _model_fields property with missing info from relationships.
  • Rebuild pydantic model schema to reflect such changes.

In your particular use-case it would be something like the following (behaviour could potentially be abstracted at SQLModel level, or at least at a base class level, so it is not performed in a per-class/per-attribute level each time):

from sqlmodel import Relationship, SQLModel, Field
from pydantic.main import BaseModel


class SpecialFieldValidator(BaseModel):
    id: int = Field(primary_key=True, index=True)

    def __init_subclass__(cls) -> None:
        # Try to figure out what type the special_field relation has
        print("special_field" in cls.__annotations__)  # False
        print("special_field" in cls.__fields__)  # False
        print("special_field" in cls.__sqlmodel_relationships__)  # True, but no type info via this dict, just a plain RelationshipInfo() instance:
        print(dict(cls.__sqlmodel_relationships__["special_field"].__dict__))  # {'back_populates': None, 'link_model': None, 'sa_relationship': None, 'sa_relationship_args': None, 'sa_relationship_kwargs': None}

        return super().__init_subclass__()

class MyModelA(SQLModel, SpecialFieldValidator, table=True):
    id: int = Field(primary_key=True)
    special_field_id: int = Field(foreign_key="mymodelb.id")
    special_field: "MyModelB" = Relationship()

    def __new__(cls, *args: Any, **kwargs: Any) -> Any:
        new_object = super().__new__(cls, *args, **kwargs)
        new_object.model_fields.update({"special_field": Field(..., description="description of your model property coming from the relationship")})
        new_object.model_rebuild(force=True)
        return new_object

class MyModelB(SQLModel, SpecialFieldValidator, table=True):
    id: int = Field(primary_key=True)
    special_field_id: int = Field(foreign_key="mymodela.id")
    special_field: MyModelA = Relationship()
   
    def __new__(cls, *args: Any, **kwargs: Any) -> Any:
        new_object = super().__new__(cls, *args, **kwargs)
        new_object.model_fields.update({"special_field": Field(..., description="description of your model property coming from the relationship")})
        new_object.model_rebuild(force=True)
        return new_object

Kind regards.

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

No branches or pull requests

2 participants