-
-
Notifications
You must be signed in to change notification settings - Fork 607
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
from __future__ import annotation and many to many example #196
Comments
Hi, I have created a similar virtual env with python 3.8.10 and this piece of code works as expected. |
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 annotations from 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... |
@l1b3r I am having some problems with the models separated in different files. Are you using this option, does it work for you? Thanks |
It looks like the forward reference is not evaluated at all, but passed to SQLAlchemy as-is. |
I experienced the same issue on version |
This is a pretty confusing problem to have given the selling point of this library. |
Postponed annotations are still an issue with I also hit this issue (again) with a new project, But removing from __future__ import annotations fixed it (again). |
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. |
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? |
While documentation is important, is there a plan or a route to fix this? Strings don't work for nice things like |
I think you can do something like this |
@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 |
Just to double check - this isn't something that would be fixed by adding |
Seems reasonable to me that it would fix the problem. |
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 annotation
by 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
The text was updated successfully, but these errors were encountered: