-
-
Notifications
You must be signed in to change notification settings - Fork 664
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
Unable to use original type hint by defining relationship #228
Comments
I am not sure I understand your question correctly, but here |
@Whitroom pls update to pydantic v1.9.1 - it should be fixed |
I meet a similar problem, class Base(SQLModel):
pass
class Test:
basemodel = Base # type alias at here, classVar[type[Base]] also didn't work
__slots__ = 'filename', 'datamodel'
def __init__(self, filename: str, datamodel: Optional[Base] = None, **kwargs):
self.filename = filename
if datamodel is not None:
assert isinstance(datamodel, self.basemodel) # self.basemodel is Any at here, should be type[Base]
self.datamodel = datamodel
else:
self.datamodel = self.basemodel(**kwargs) # self.basemodel is Any at here, should be type[Base] If you remove inheritance of Base, typehint work perfectly. class Base(SQLModel, table=True):
@classmethod
def get_instance(cls, data: dict):
return Base(**data)
class Test:
basemodel = Base.get_instance
__slots__ = 'filename', 'datamodel'
def __init__(self, filename: str, datamodel: Optional[Base] = None, **kwargs):
self.filename = filename
if datamodel is not None:
assert isinstance(datamodel, self.basemodel)
self.datamodel = datamodel
else:
self.datamodel = self.basemodel(**kwargs) Btw, filename is private attribute of Base at first, but idk how to init it. |
Many to one relationship using sqlmodel and sqlalchemy along with alembic
I am facing an issue with many to one relationship. Having followed the official documentation, I created my relationship attributes inside my table. from sqlmodel import SQLModel
from sqlmodel import Field
from sqlmodel import Relationship
from typing import Optional
from typing import List
class Category(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str = Field(..., title="Category name")
subcategories: List['SubCategory'] = Relationship(back_populates='category')
class SubCategory(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str = Field(..., title="Sub category name")
category_id: int | None = Field(default=None, foreign_key='category.id')
category: 'Category' | None = Relationship(back_populates='subcategories') |
Some updates here: I tested the tutorial code with the sample provided and found that using from future import annotations might cause a mapper error. After removing the import, it worked.
Import annotations might cause the mapper errorIf in my model file got import annotations, then i will get the mappers error, after i remove Tutorial i refer to - https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/define-relationships-attributes/#declare-relationship-attributes Context & Environmentspython 3.12 |
First Check
Commit to Help
Example Code
Description
I create Hero and Team models and try to use python original type hint, but while running it, it occurs exceptions...
Traceback (most recent call last):
File "pydantic\validators.py", line 709, in pydantic.validators.find_validators
TypeError: issubclass() arg 1 must be a class
Traceback (most recent call last):
File "C:\Users\10620\Desktop\Code\Python\sqlmodel\table_relationship.py", line 5, in
class Team(SQLModel, table=True):
File "C:\Users\10620\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlmodel\main.py", line 342, in init
temp_field = ModelField.infer(
File "pydantic\fields.py", line 488, in pydantic.fields.ModelField.infer
File "pydantic\fields.py", line 419, in pydantic.fields.ModelField.init
File "pydantic\fields.py", line 534, in pydantic.fields.ModelField.prepare
File "pydantic\fields.py", line 728, in pydantic.fields.ModelField._type_analysis
File "pydantic\fields.py", line 778, in pydantic.fields.ModelField._create_sub_type
File "pydantic\fields.py", line 419, in pydantic.fields.ModelField.init
File "pydantic\fields.py", line 539, in pydantic.fields.ModelField.prepare
File "pydantic\fields.py", line 801, in pydantic.fields.ModelField.populate_validators
File "pydantic\validators.py", line 718, in find_validators
RuntimeError: error checking inheritance of 'Hero' (type: str)
The solution is adding List from typing and replacing it, but I don't know how to solve this bug...
link: https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/define-relationships-attributes/
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10.1
Additional Context
No response
The text was updated successfully, but these errors were encountered: