Skip to content
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

Mention StaticPool for in-memory db with FastAPI #506

Closed
wants to merge 2 commits into from

Conversation

abrahammurciano
Copy link

To use a :memory: database in a multithreaded scenario, the same connection object must be shared among threads, since the database exists only within the scope of that connection. The StaticPool implementation will maintain a single connection globally

https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#using-a-memory-database-in-multiple-threads

I think it is important that we mention this here, as anyone trying to learn to use FastAPI/SQLModel will likely start by using an in-memory database. If they are unaware of this detail, they will encounter very confusing bugs which will be near impossible to debug.

> To use a :memory: database in a multithreaded scenario, the same connection object must be shared among threads, since the database exists only within the scope of that connection. The StaticPool implementation will maintain a single connection globally

https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#using-a-memory-database-in-multiple-threads

I think it is important that we mention this here, as anyone trying to learn to use FastAPI/SQLModel will likely start by using an in-memory database. If they are unaware of this detail, they will encounter very confusing bugs which will be near impossible to debug.
@github-actions
Copy link

📝 Docs preview for commit 5c85706 at: https://637df0c0501ee11434d75902--sqlmodel.netlify.app

@github-actions
Copy link

📝 Docs preview for commit 54cee8a at: https://639cdfe91f184e00743a2cdd--sqlmodel.netlify.app

@tiangolo tiangolo added the docs Improvements or additions to documentation label Oct 22, 2023
@tiangolo
Copy link
Member

Thanks!

I would expect people following the tutorial would follow it as-is, with a database in a file, that can also be explored using SQLite Explorer.

Then I would expect them to use in-memory DBs for testing, as it's a more advanced feature. And it is documented in that section: https://sqlmodel.tiangolo.com/tutorial/fastapi/tests/#configure-the-in-memory-database

So I'll pass on this, but thanks for the interest! ☕

@tiangolo tiangolo closed this Oct 23, 2023
@abrahammurciano abrahammurciano deleted the patch-1 branch November 1, 2023 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants