-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Depends object returned instead of session #1693
Comments
As I can see, you calling
|
Just to complement:
Notice that As @SirTelemak said, you should use this notation:
And here you can see that Ps.: You can still use the last notation, without
or:
|
Why do you not recommend this approach? My first example was like the example you put. I ended getting the initial approach cause I was declaring in its own module. I ended up reading somewhere that it might be because it was outside of my routes but I did not dig further yet since I was unsure if sqlalchemy's way was enough for my use case. |
First of all, that will be executed only once when this function imported, so you losing your "one session for one call" functionality and if something happens during handling db request you can lose session and won't be able to reopen it again. So, with this approach you lose versatility and control in your program, |
Third reason not to use that approach is that you lose the dependency injection/substitution feature of FastAPI, which makes it very easy to swap out dependencies when running tests, for example to use a testing database. |
@SirTelemak I read more into the docs and the replies and I understand the benefits much more now. I was confused so I had to step away and read through everything again to see why my way wasn't working how I wanted. I was able to find a way to make the session recreate if I wanted to do it using reload so my functions would be reimported and I would get a new connection. @GabrielCarpr, however, brought up testing. It be harder with my approach cause then pytest would need to know how to configure my database for testing so I would lose any benefits I thought I would be achieving. Thank you all for the help. |
I'm still receiving Note that using My
I've sent so many flags to create_engine because I'm finding that FastAPI/SQLAlchemy crashes when I'm making multiple requests in a fast timeframe. Why is this issue still occurring and how can it be resolved? |
@H0r53 can you include a full example, including your route? |
Here is an example that issues simple token authentication derived from the FastAPI examples. The notable code for Edited to show relevant code
|
@H0r53 you can't use Depends in your own functions, it has to be in FastAPI functions, mainly routes. You can, however, use Depends in your own functions when that function is also a dependency, so could can have a chain of functions. Eg, a route uses Depends to resolve a 'get_current_user', which also uses Depends to resolve 'get_db', and the whole chain will be resolved. But if you then call 'get_current_user' without using Depends, it won't be able to resolve 'get_db'. What I do is get the DB session from the route and then pass it down through every layer and function. I believe this is also better design. |
So just for clarity, only use Depends on routes? |
Yes, unless you're using it in a function that itself is resolved by Depends. But for simplicity, keep it in the route |
This has been a nightmare over the past several months involving several roundabout "solutions." After following your advice everything appears to be working and I'm hoping this resolves the other strange session related issues that I've been experiencing. Thank you! |
@H0r53 I went through the same, including |
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue 👍 |
Hi, how should I handle then getting a session outside FastAPI route functions? Like on startUp I get some data from a SQL DB - putting the functionality in a route makes no sense to me. Thanks :) |
@CommonCrisis My recommendation is to use the context manager way: https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/#using-context-managers-in-dependencies-with-yield |
This is a very good comment which should be somehow included in https://fastapi.tiangolo.com/tutorial/dependencies/. I'm pretty angry that the official page fails to mention this very concrete limitation - even though it makes perfect sense from the code execution point of view. The documentation tries to conceal how the code is executed - while adding the insult of stressing the simplicity and effortlessness, making the document readers having failed to use the feature feel themselves complete morons. The expectation of magic fails, since DI does not get invoked in most functions in a typical program (which is natural when thought out with realistic expectations regarding magic in programming). I'd expect to see when the DI machinery is actually triggered, even though it means loosing the illusion of unicorns and magic. I have so far every time failed to use use the DI in one of our applications, without having any support from the documentation. |
Feel free to provide a comment to the documentation with a PR. I would disagree with most of your statements and nowhere in the docs is implied that the DI system of FastAPI works outside the context of FastAPI. If you think its insulting that the easiness of said methodology is stressed in the docs, then you might be able to rephrase those bits as well if your at it :) |
I'm sorry, but I can't agree I could write any acceptable documentation about a feature that I still don't understand after several months of fighting against. I think it would be wasting anyone's time. I'm personally very much opposed to "intuitive and powerful" implicit machinery, they don't fit my mental model. They set expectations which don't hold for first time users if something changes or goes wrong. That is my personal limitation which can't be generalised without additional evidence. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I am currently trying to migrate from flask to fastapi and trying to keep my files separated. My depends is not called from my routes but from within my crud file but similar to the documentation for relational databases: https://fastapi.tiangolo.com/tutorial/sql-databases/
This was my initial way of using a database session which is similar to the sqlalchemy docs
Also, is this bad practice or is it okay to use this and a mix of depends? I did kind of want to decouple sqlalchemy from fastapi.
I then tried to switch and use Depends but am getting the same issue as others below with my attempts added at the end:
First error I got:
Without Depends:
Originally posted by @BrainABar in #631 (comment)
The text was updated successfully, but these errors were encountered: