-
-
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
migration to sqlalchemy 2.0 #563
Conversation
📝 Docs preview for commit 60318b4 at: https://6406825e11187c06ceaecaf0--sqlmodel.netlify.app |
@farahats9, great! Do you think you would be able to sort out the failing tests? :) |
I fixed some of the |
📝 Docs preview for commit b48423f at: https://641d9f2736a7fa1c9568bdb7--sqlmodel.netlify.app |
I used this version in my project with env: |
Co-authored-by: Stefan Borer <[email protected]>
📝 Docs preview for commit 9c219d9 at: https://6426bd112dc67b078b6e5298--sqlmodel.netlify.app |
📝 Docs preview for commit 0eee8e8 at: https://6426be4468302c086218a5bb--sqlmodel.netlify.app |
📝 Docs preview for commit 050cf02 at: https://6426c1ce9df2a80bfd37ae93--sqlmodel.netlify.app |
@farahats9 someone created a PR on your fork for fixing the mypy issues |
This seems good to go just needs merging |
We are very much waiting for this change |
Why do you wait, instead of taking it to use? I guess feedback from users would be helpful info also for the maintainer to know when to merge. |
🚀 👀👀 |
Great! I would love to try this branch in my production environment. |
Would like to see this merged for asyncpg support. Asyncpg seems to greatly increase performance based on most simple benchmarks. |
@k-s-t-i You can currently use Asyncpg with sqlalchemy v1.4 and sqlmodel v0.0.8 with no issue in most cases. you will need to watch out for some problems like lazy loading and cascading deletes on the orm side. |
Update: This patch can work with asyncmy in a real project. Will try asyncpg later. |
Update: asyncpg works too. Tested with a real project on both windows and linux. Notice: to make relationship work, you may use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really would like to use it in my new project! Thank you for the PR!
Five months have been passed since the release of SQLalchemy2.0, https://www.sqlalchemy.org/blog/2023/01/26/sqlalchemy-2.0.0-released/ |
You can help by using this PR in your project(s) if you want, and reporting how well it works. |
Been working with this PR for almost a month now, works great. Thank you! |
Sorry but changing the order of classes definition does not solve the error for me. At this point, I don't know what to do, but this branch is useless for me. I already have done what you mentioned, but the error persists. It always says My project has about 30 tables at this point, and it has been running for quite 1 year. My project does not run The error messages are not helpful at all. And they only happen in this specific PR. I understand it may be working for a very simple case like you presented, but for me it is always showing these errors, no matter how much code I change: py", line 2479, in get_property
return self._props[key]
KeyError: 'user' or annotation = eval(expression, base_globals, locals_)
File "<string>", line 1, in <module>
NameError: name 'PostUserLike' is not defined or .py", line 223, in eval_expression
raise NameError(
NameError: Could not de-stringify annotation 'PostUserLike' or ", line 2342, in _extract_mapped_subtype
if raiseerr and "Mapped[" in raw_annotation: # type: ignore
TypeError: argument of type 'ForwardRef' is not iterable My current API is running perfectly with If someone wants to investigate the issue with me, I am willing to screenshare and show the project structure and try to make it to work; my Discord is But one thing I wonder, even if I were able to make this PR to work on my code base after countless changes, I don't think this is how library updates are supposed to work? Every time I type This PR is certainly introducing breaking changes but I'm unable to tell what and why exactly, although I investigated a lot here I couldn't even make my project to run with it. By the way I woud love to be using |
@FilipeMarch you are right there is an issue in I feel this is a priority to fix over the mypy linting, I will do my best to figure it out but any help is appreciated. |
I had an error # models/__init__.py
# need for alembic
from . import (
company,
location,
client
) # models/company.py
if TYPE_CHECKING:
from models.location import Location
from models.client import Client
class Company(BaseModel, CompanyBase, table=True):
locations: List["Location"] = Relationship(back_populates="company")
clients: List["Client"] = Relationship(back_populates="company") # models/location.py
from models.company import Company
class Location(BaseModel, OrderedList, LocationBase, table=True):
company_id: int = Field(default=None, foreign_key="company.id")
company: Company = Relationship(back_populates="locations") # models/client.py
from models.company import Company
class Client(ClientBase, BaseModel, table=True):
company_id: int = Field(default=None, nullable=False, foreign_key="company.id")
company: Company = Relationship(back_populates="clients") |
Hey @farahats9, I'm getting error when using sqlalchemy relationship: class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
powers: List["Power"] = Relationship(
sa_relationship=relationship(back_populates='hero')
)
class Power(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
hero_id: int = Field(foreign_key="hero.id")
hero: "Hero" = Relationship(back_populates="powers")
any idea why? |
@matanper I think the forward Relationship (with the Foreign Key) needs to be a proper class, and not a type string. Can you try this? class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
powers: List["Power"] = Relationship(
sa_relationship=relationship(back_populates='hero')
)
class Power(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
hero_id: int = Field(foreign_key="hero.id")
hero: Hero = Relationship(back_populates="powers") # <== No quotes around Hero |
I've looked at this PR a bit to try cleaning up the linting errors. I've reduced most of them, but q = User.name == "spongebob"
This shows up as an error, but is correct at runtime. At runtime, we see an |
Fix most linting errors
Environment:
pytest errors:
|
TL;DR: Good news, this is available in the just released SQLModel 0.0.12! 🎉 Awesome, thanks a lot for all the work @farahats9! 🚀 And thanks everyone for the comments, reviews, etc. ☕ @farahats9 I continued the work here with several things on top on PR #700. I took your commits so you're co-author of that PR (and contributor to SQLModel, check your badge here next to your username, or in https://github.com/tiangolo/sqlmodel/graphs/contributors 😎 ). I did a few extra things:
It is now available in SQLModel 0.0.12. 🎉 As this is now included in the repo and released, I'll now close this PR. Thanks! 🍰 |
Hope it can help : I had the same issue because the relationsship was always defined and I didn' decalre it Optional : |
made the required changes to pass the tests for sqlalchemy 2.0
python version had to be pumped to 3.7 instead of 3.6
removed sqlalchemy2-stubs since it is not compatible or required.
More testing in a real project scenario is welcome.