Replies: 18 comments
-
|
Hi, I have created a similar virtual env with python 3.8.10 and this piece of code works as expected. |
Beta Was this translation helpful? Give feedback.
-
|
I had a similar issue with 1-M relationship, where the models are also separated into their own files. Not sure if that helps, but I was able to workaround this issue by removing from __future__ import annotationsfrom the file of the "parent" model and returning back to quoting the type hints: # other imports omitted
from typing import TYPE_CHECKING:
from .child import ChildModel
class ParentModel(SQLModel, table=True):
# children: List[ChildModel] = Relationship(back_populates="parent") # does not work
children: List["ChildModel"] = Relationship(back_populates="parent") # OK
# other column definitions... |
Beta Was this translation helpful? Give feedback.
-
@l1b3r I am having some problems with the models separated in different files. Are you using this option, does it work for you? Thanks |
Beta Was this translation helpful? Give feedback.
-
|
It looks like the forward reference is not evaluated at all, but passed to SQLAlchemy as-is. |
Beta Was this translation helpful? Give feedback.
-
|
I experienced the same issue on version |
Beta Was this translation helpful? Give feedback.
-
|
This is a pretty confusing problem to have given the selling point of this library. |
Beta Was this translation helpful? Give feedback.
-
|
Postponed annotations are still an issue with I also hit this issue (again) with a new project, But removing from __future__ import annotationsfixed it (again). |
Beta Was this translation helpful? Give feedback.
-
|
Thank you @s-weigand, I removed the Would have saved me time if I was told that future annotations cannot be used, and that forward type references in SQLModel classes should be enclosed in double quotes to play nice with SA. I guess I should have gleaned this from the docs, on the page about type annotation strings? Considering this issue thread though, I think it's worth addressing explicitly somewhere. I'll look into adding this caveat to the docs if contributions are accepted. |
Beta Was this translation helpful? Give feedback.
-
|
yeah i lost loads of time to this, definitely think 'don't import annotations from future' should be stated somewhere in the docs. unless i missed it? |
Beta Was this translation helpful? Give feedback.
-
|
While documentation is important, is there a plan or a route to fix this? Strings don't work for nice things like |
Beta Was this translation helpful? Give feedback.
-
I think you can do something like this |
Beta Was this translation helpful? Give feedback.
-
|
@DirkReiners Forward referenced type annotations are a planned future feature for Python, so I wouldn't expect this library to implement a fix. I explain the situation and state of the feature in a bit more detail in my PR |
Beta Was this translation helpful? Give feedback.
-
|
Just to double check - this isn't something that would be fixed by adding |
Beta Was this translation helpful? Give feedback.
-
|
Seems reasonable to me that it would fix the problem. |
Beta Was this translation helpful? Give feedback.
-
|
Having the same issue.
Although this workaround works for the time being, the code will no longer work in the future (when __future__.annotations become the default), so this should be fixed. |
Beta Was this translation helpful? Give feedback.
-
|
It is possible to do the Many to Many example from the initial post with future annotations by overwriting the relationships with sqlalchemy's sa_relatiopnships. The following code works and the only changes from the original post are changing Obviously it is a bit of a pain to change the SQLModel Relationship to a raw sqlalchemy relationship, but at least then it works... Note: Obviously this only fixes the original M-M issue. I still have issues with M-M or 1-M or M-1 when models are in different files... for now if I import |
Beta Was this translation helpful? Give feedback.
-
|
As the comment directly above mentioned, this is becoming more of an issue in Python 3.14 (which has been released by now), where unquoted forward annotations don't even need a |
Beta Was this translation helpful? Give feedback.
-
|
This is actually something I’ve encounter quite a bit in my code. It hasn't broken for me yet. I'm using SQLModel v0.0.38 and Python v3.14.4. So I suppose it is possible some of the underlying code that made this a problem before has since been refactored. When it comes to have the models in different files, @l1b3r’s solution above was actually very close. Here is what I do: # This example assumes Python >=3.14, so no __future__ import here
from typing import TYPE_CHECKING
from sqlmodel import Relationship, SQLModel
if TYPE_CHECKING:
from .child import ChildModel
class ParentModel(SQLModel, table=True):
children: list[ChildModel] = Relationship(back_populates="parent")This is how my code is structured, and it works great for me. Because the And as far as having them in the same file goes, I haven't had much trouble with that either. The only problem there is that if you use a model for anything other than a type hint (i.e. So it could be working for me just because I'm on newer versions of Python and SQLModel. But theoretically, even using slightly older versions of Python and using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
all what i have done is use the example from the docs about many to many relationships and tried to fit them with
from __future__ import annotationby adding the import in the first line and changingList["Hero"]toList[Hero]. and i get this erroralso
update_forward_refs()did not help for both classes.Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
Python 3.9.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions